Skip to content

Commit

Permalink
Cleanup and fix for not retrieving all subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
test@dev.kiener.nl committed Mar 18, 2020
1 parent d6db554 commit f6fab82
Show file tree
Hide file tree
Showing 14 changed files with 1,160 additions and 1,304 deletions.
261 changes: 108 additions & 153 deletions Components/Api/Resource/Quote.php
Original file line number Diff line number Diff line change
@@ -1,153 +1,108 @@
<?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;
}
}
<?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 = $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;
}
}
Loading

0 comments on commit f6fab82

Please sign in to comment.