Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
m-a-x-s-e-e-l-i-g committed Jan 3, 2019
0 parents commit c591bfb
Show file tree
Hide file tree
Showing 12 changed files with 1,305 additions and 0 deletions.
153 changes: 153 additions & 0 deletions Components/Api/Resource/Quote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Shopware\Components\Api\Resource;

use Shopware\Components\Api\Exception as ApiException;
use MailCampaignsConnector\Models\Quote\Quote as QuoteModel;

/**
* Class Quote
*
* @package Shopware\Components\Api\Resource
*/
class Quote extends Resource
{
/**
* @return \MailCampaignsConnector\Models\Quote\Repository
*/
public function getRepository()
{
return $this->getManager()->getRepository(QuoteModel::class);
}

/**
* @param int $offset
* @param int $limit
* @param array $criteria
* @param array $orderBy
* @return array
*/
public function getList($offset = 0, $limit = 25, array $criteria = [], array $orderBy = [])
{
//$builder = Shopware()->Models()->createQueryBuilder();
//$builder->select(['quote'])->from(\MailCampaignsConnector\Models\Quote\Quote::class, 'quote');

$builder = $this->getRepository()->createQueryBuilder('quote');

$builder
->addFilter($criteria)
->addOrderBy($orderBy)
->setFirstResult($offset)
->setMaxResults($limit);

$query = $builder->getQuery();
$query->setHydrationMode($this->resultMode);
$sql = $query->getSql();

$paginator = $this->getManager()->createPaginator($query);

//returns the total count of the query
$totalResult = $paginator->count();

//returns the Quote data
$quotes = $paginator->getIterator()->getArrayCopy();
$newquotes = array();
$quotedates = array();
$i=0;
foreach($quotes as $quote)
{
$newquotes[$quote["sessionId"]]["customerId"] = $quote["customerId"];
$newquotes[$quote["sessionId"]]["sessionId"] = $quote["sessionId"];
$newquotes[$quote["sessionId"]]["dateChanged"] = $quote["date"];

/* Creation date */
if ($newquotes[$quote["sessionId"]]["dateCreated"] == "")
$newquotes[$quote["sessionId"]]["dateCreated"] = $quote["date"];

if (strtotime($quote["date"]) < strtotime($newquotes[$quote["sessionId"]]["dateCreated"]))
{
$newquotes[$quote["sessionId"]]["dateCreated"] = $quote["date"];
}
/* Creation date */

$quote["customerId"] = $quote["customerId"];
$newquotes[$quote["sessionId"]]["details"][] = $quote;
}

return ['data' => $newquotes, 'total' => $totalResult]; // , 'sql' => $sql,
}

/**
* Get One Quote Information
*
* @param $id
* @return mixed
* @throws ApiException\NotFoundException
* @throws ApiException\ParameterMissingException
*/
public function getOne($id)
{
$this->checkPrivilege('read');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
}

$builder = $this->getRepository()
->createQueryBuilder('Quote')
->select('Quote')
->where('Quote.id = ?1')
->setParameter(1, $id);

/** @var QuoteModel $quote */
$quote = $builder->getQuery()->getOneOrNullResult($this->getResultMode());

if (!$quote) {
throw new ApiException\NotFoundException("Quote by id $id not found");
}

return $quote;
}

/**
* @param $id
* @param array $params
* @return null|object
* @throws ApiException\ValidationException
* @throws ApiException\NotFoundException
* @throws ApiException\ParameterMissingException
*/
public function update($id, array $params)
{
$this->checkPrivilege('update');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
}

/** @var $quote QuoteModel */
$builder = $this->getRepository()
->createQueryBuilder('Quote')
->select('Quote')
->where('Quote.id = ?1')
->setParameter(1, $id);

/** @var QuoteModel $quote */
$quote = $builder->getQuery()->getOneOrNullResult(self::HYDRATE_OBJECT);

if (!$quote) {
throw new ApiException\NotFoundException("Quote by id $id not found");
}

$quote->fromArray($params);

$violations = $this->getManager()->validate($quote);
if ($violations->count() > 0) {
throw new ApiException\ValidationException($violations);
}

$this->flush();

return $quote;
}
}
195 changes: 195 additions & 0 deletions Components/Api/Resource/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace Shopware\Components\Api\Resource;

use Shopware\Components\Api\Exception as ApiException;
use MailCampaignsConnector\Models\Subscriber\Subscriber as SubscriberModel;

/**
* Class Subscriber
*
* @package Shopware\Components\Api\Resource
*/
class Subscriber extends Resource
{
/**
* @return \MailCampaignsConnector\Models\Subscriber\Repository
*/
public function getRepository()
{
return $this->getManager()->getRepository(SubscriberModel::class);
}

/**
* Create new Subscriber
*
* @param array $params
* @return SubscriberModel
* @throws ApiException\ValidationException
*/
public function create(array $params)
{
/** @var SubscriberModel $subscriber */
$subscriber = new SubscriberModel();

$subscriber->fromArray($params);

$violations = $this->getManager()->validate($subscriber);

/**
* Handle Violation Errors
*/
if ($violations->count() > 0) {
throw new ApiException\ValidationException($violations);
}

$this->getManager()->persist($subscriber);
$this->flush();

return $subscriber;
}

/**
* @param int $offset
* @param int $limit
* @param array $criteria
* @param array $orderBy
* @return array
*/
public function getList($offset = 0, $limit = 25, array $criteria = [], array $orderBy = [])
{
// $builder = Shopware()->Models()->createQueryBuilder();
// $builder->select(['s.id, s.email, s'])
// ->from(\MailCampaignsConnector\Models\Subscriber\Subscriber::class, 's');
// $results = $builder->getQuery()->getArrayResult();
// $getDql = $builder->getDql();
// $getSql = $builder->getQuery()->getSql();
// return ['data' => $results, 'getDql' => $getDql, 'getSql' => $getSql];

$builder = $this->getRepository()->createQueryBuilder('subscriber');
$builder->addFilter($criteria)
->addOrderBy($orderBy)
->setFirstResult($offset)
->setMaxResults($limit);
$query = $builder->getQuery();
$query->setHydrationMode($this->resultMode);

$paginator = $this->getManager()->createPaginator($query);

//returns the total count of the query
$totalResult = $paginator->count();

//returns the Subscriber data
$subscriber = $paginator->getIterator()->getArrayCopy();
// ookcool
// $subscriber = $query->getSingleResult();
// $subscriber = $query->getArrayResult();
// $subscriber = $query->getScalarResult();
// $subscriber = $query->getSingleScalarResult();

$getDql = $builder->getDql();
$getSql = $builder->getQuery()->getSql();

return ['data' => $subscriber, 'total' => $totalResult];
}

/**
* Delete Existing Subscriber
*
* @param $id
* @return null|object
* @throws ApiException\NotFoundException
* @throws ApiException\ParameterMissingException
*/
public function delete($id)
{
$this->checkPrivilege('delete');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
}

$subscriber = $this->getRepository()->find($id);

if (!$subscriber) {
throw new ApiException\NotFoundException("Subscriber by id $id not found");
}

$this->getManager()->remove($subscriber);
$this->flush();
}

/**
* Get One Subscriber Information
*
* @param $id
* @return mixed
* @throws ApiException\NotFoundException
* @throws ApiException\ParameterMissingException
*/
public function getOne($id)
{
$this->checkPrivilege('read');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
}

$builder = $this->getRepository()
->createQueryBuilder('Subscriber')
->select('Subscriber')
->where('Subscriber.id = ?1')
->setParameter(1, $id);

/** @var SubscriberModel $subscriber */
$subscriber = $builder->getQuery()->getOneOrNullResult($this->getResultMode());

if (!$subscriber) {
throw new ApiException\NotFoundException("Subscriber by id $id not found");
}

return $subscriber;
}

/**
* @param $id
* @param array $params
* @return null|object
* @throws ApiException\ValidationException
* @throws ApiException\NotFoundException
* @throws ApiException\ParameterMissingException
*/
public function update($id, array $params)
{
$this->checkPrivilege('update');

if (empty($id)) {
throw new ApiException\ParameterMissingException();
}

/** @var $subscriber SubscriberModel */
$builder = $this->getRepository()
->createQueryBuilder('Subscriber')
->select('Subscriber')
->where('Subscriber.id = ?1')
->setParameter(1, $id);

/** @var SubscriberModel $subscriber */
$subscriber = $builder->getQuery()->getOneOrNullResult(self::HYDRATE_OBJECT);

if (!$subscriber) {
throw new ApiException\NotFoundException("Subscriber by id $id not found");
}

$subscriber->fromArray($params);

$violations = $this->getManager()->validate($subscriber);
if ($violations->count() > 0) {
throw new ApiException\ValidationException($violations);
}

$this->flush();

return $subscriber;
}
}
Loading

0 comments on commit c591bfb

Please sign in to comment.