* lib update

* static analyzer added
* new tags
* logger improvements
This commit is contained in:
Danyi Dávid
2018-12-03 22:13:57 +01:00
parent a49577dfad
commit b1f6daf424
39 changed files with 1387 additions and 1367 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ResponseInterface;
@@ -36,6 +38,8 @@ abstract class AbstractAction implements RequestHandlerInterface
return $this->options($request);
case 'PATCH':
return $this->patch($request);
default:
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Action;
use App\Service\KoinService;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Action;
use App\Service\KoinService;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ResponseInterface;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Action;
use App\Entity\Sms;
@@ -17,12 +19,19 @@ class StoreAction extends AbstractAction
$this->smsStore = $smsStore;
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function create(ServerRequestInterface $request): ResponseInterface
{
$hashKey = $request->getAttribute('hashKey');
$direction = $request->getAttribute('direction');
$mappedDirection = Sms::MAP_DIRECTIONS[$direction];
$requestData = $this->getRequestData($request);
$isStored = $this->smsStore->storeSms($hashKey, $mappedDirection, $requestData);
return new JsonCorsResponse($isStored);
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Action;
use App\Service\SmsStoreService;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\Sms;
@@ -42,6 +44,13 @@ class KoinImportCommand extends Command
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$smsId = $input->getArgument('id');

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Service\KoinService;

View File

@@ -1,10 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Service\SZEPManagerService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -28,9 +29,16 @@ class PeriodicSZEPCommand extends Command
->setDescription('Parse SZEP card');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$result = $this->szepManager->pollRecent();
$output->writeln($result);
$this->szepManager->pollRecent();
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Service\SZEPManagerService;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App;
use Zend\EventManager\EventManager;

View File

@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping as ORM; // @phan-suppress-current-line PhanUnreferencedUseNormal
/**
* @ORM\Entity

View File

@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping as ORM; // @phan-suppress-current-line PhanUnreferencedUseNormal
/**
* @ORM\Entity
@@ -72,6 +74,12 @@ class Sms implements \JsonSerializable
*/
private $owner;
/**
* @ORM\Column(name="parsed_and_handled", type="boolean", options={"default" = true})
* @var bool
*/
private $parsedAndHandled;
/**
* @return int
*/
@@ -198,6 +206,24 @@ class Sms implements \JsonSerializable
return $this;
}
/**
* @return bool
*/
public function isParsedAndHandled(): bool
{
return $this->parsedAndHandled;
}
/**
* @param bool $parsedAndHandled
* @return Sms
*/
public function setParsedAndHandled(bool $parsedAndHandled): Sms
{
$this->parsedAndHandled = $parsedAndHandled;
return $this;
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
@@ -215,6 +241,7 @@ class Sms implements \JsonSerializable
'when' => $this->getOccuredAt(),
'direction' => $this->getDirection(),
'owner' => $this->getOwner(),
'isParsedAndHandled' => $this->isParsedAndHandled(),
];
}
}

View File

@@ -1,9 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping as ORM; // @phan-suppress-current-line PhanUnreferencedUseNormal
/**
* @ORM\Entity
@@ -103,7 +105,7 @@ class User implements \JsonSerializable
}
/**
* @return Sms[]|ArrayCollection
* @return ArrayCollection|Sms[]
*/
public function getSmsMessages(): ?ArrayCollection
{
@@ -111,7 +113,7 @@ class User implements \JsonSerializable
}
/**
* @param Sms[]|ArrayCollection $smsMessages
* @param ArrayCollection|null $smsMessages
* @return User
*/
public function setSmsMessages(?ArrayCollection $smsMessages): User

View File

@@ -1,594 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator;
use DateTime;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Util\Inflector;
use InvalidArgumentException;
use RuntimeException;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Hydrator\AbstractHydrator;
use Zend\Hydrator\Filter\FilterProviderInterface;
/**
* This hydrator has been completely refactored for DoctrineModule 0.7.0. It provides an easy and powerful way
* of extracting/hydrator objects in Doctrine, by handling most associations types.
*
* Starting from DoctrineModule 0.8.0, the hydrator can be used multiple times with different objects
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class DoctrineObject extends AbstractHydrator
{
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var ClassMetadata
*/
protected $metadata;
/**
* @var bool
*/
protected $byValue = true;
/**
* Constructor
*
* @param ObjectManager $objectManager The ObjectManager to use
* @param bool $byValue If set to true, hydrator will always use entity's public API
*/
public function __construct(ObjectManager $objectManager, $byValue = true)
{
parent::__construct();
$this->objectManager = $objectManager;
$this->byValue = (bool) $byValue;
}
/**
* Extract values from an object
*
* @param object $object
* @return array
*/
public function extract($object)
{
$this->prepare($object);
if ($this->byValue) {
return $this->extractByValue($object);
}
return $this->extractByReference($object);
}
/**
* Hydrate $object with the provided $data.
*
* @param array $data
* @param object $object
* @return object
*/
public function hydrate(array $data, $object)
{
$this->prepare($object);
if ($this->byValue) {
return $this->hydrateByValue($data, $object);
}
return $this->hydrateByReference($data, $object);
}
/**
* Prepare the hydrator by adding strategies to every collection valued associations
*
* @param object $object
* @return void
*/
protected function prepare($object)
{
$this->metadata = $this->objectManager->getClassMetadata(get_class($object));
$this->prepareStrategies();
}
/**
* Prepare strategies before the hydrator is used
*
* @throws \InvalidArgumentException
* @return void
*/
protected function prepareStrategies()
{
$associations = $this->metadata->getAssociationNames();
foreach ($associations as $association) {
if ($this->metadata->isCollectionValuedAssociation($association)) {
// Add a strategy if the association has none set by user
if (!$this->hasStrategy($association)) {
if ($this->byValue) {
$this->addStrategy($association, new Strategy\AllowRemoveByValue());
} else {
$this->addStrategy($association, new Strategy\AllowRemoveByReference());
}
}
$strategy = $this->getStrategy($association);
if (!$strategy instanceof Strategy\AbstractCollectionStrategy) {
throw new InvalidArgumentException(
sprintf(
'Strategies used for collections valued associations must inherit from '
. 'Strategy\AbstractCollectionStrategy, %s given',
get_class($strategy)
)
);
}
$strategy->setCollectionName($association)
->setClassMetadata($this->metadata);
}
}
}
/**
* Extract values from an object using a by-value logic (this means that it uses the entity
* API, in this case, getters)
*
* @param object $object
* @throws RuntimeException
* @return array
*/
protected function extractByValue($object)
{
$fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
$methods = get_class_methods($object);
$filter = $object instanceof FilterProviderInterface
? $object->getFilter()
: $this->filterComposite;
$data = [];
foreach ($fieldNames as $fieldName) {
if ($filter && !$filter->filter($fieldName)) {
continue;
}
$getter = 'get' . Inflector::classify($fieldName);
$isser = 'is' . Inflector::classify($fieldName);
$dataFieldName = $this->computeExtractFieldName($fieldName);
if (in_array($getter, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$getter(), $object);
} elseif (in_array($isser, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$isser(), $object);
} elseif (substr($fieldName, 0, 2) === 'is'
&& ctype_upper(substr($fieldName, 2, 1))
&& in_array($fieldName, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$fieldName(), $object);
}
// Unknown fields are ignored
}
return $data;
}
/**
* Extract values from an object using a by-reference logic (this means that values are
* directly fetched without using the public API of the entity, in this case, getters)
*
* @param object $object
* @return array
*/
protected function extractByReference($object)
{
$fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
$refl = $this->metadata->getReflectionClass();
$filter = $object instanceof FilterProviderInterface
? $object->getFilter()
: $this->filterComposite;
$data = [];
foreach ($fieldNames as $fieldName) {
if ($filter && !$filter->filter($fieldName)) {
continue;
}
$reflProperty = $refl->getProperty($fieldName);
$reflProperty->setAccessible(true);
$dataFieldName = $this->computeExtractFieldName($fieldName);
$data[$dataFieldName] = $this->extractValue($fieldName, $reflProperty->getValue($object), $object);
}
return $data;
}
/**
* Hydrate the object using a by-value logic (this means that it uses the entity API, in this
* case, setters)
*
* @param array $data
* @param object $object
* @throws RuntimeException
* @return object
*/
protected function hydrateByValue(array $data, $object)
{
$tryObject = $this->tryConvertArrayToObject($data, $object);
$metadata = $this->metadata;
if (is_object($tryObject)) {
$object = $tryObject;
}
foreach ($data as $field => $value) {
$field = $this->computeHydrateFieldName($field);
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
$setter = 'set' . Inflector::classify($field);
if ($metadata->hasAssociation($field)) {
$target = $metadata->getAssociationTargetClass($field);
if ($metadata->isSingleValuedAssociation($field)) {
if (! method_exists($object, $setter)) {
continue;
}
$value = $this->toOne($target, $this->hydrateValue($field, $value, $data));
if (null === $value
&& !current($metadata->getReflectionClass()->getMethod($setter)->getParameters())->allowsNull()
) {
continue;
}
$object->$setter($value);
} elseif ($metadata->isCollectionValuedAssociation($field)) {
$this->toMany($object, $field, $target, $value);
}
} else {
if (! method_exists($object, $setter)) {
continue;
}
$object->$setter($this->hydrateValue($field, $value, $data));
}
}
return $object;
}
/**
* Hydrate the object using a by-reference logic (this means that values are modified directly without
* using the public API, in this case setters, and hence override any logic that could be done in those
* setters)
*
* @param array $data
* @param object $object
* @return object
*/
protected function hydrateByReference(array $data, $object)
{
$tryObject = $this->tryConvertArrayToObject($data, $object);
$metadata = $this->metadata;
$refl = $metadata->getReflectionClass();
if (is_object($tryObject)) {
$object = $tryObject;
}
foreach ($data as $field => $value) {
$field = $this->computeHydrateFieldName($field);
// Ignore unknown fields
if (!$refl->hasProperty($field)) {
continue;
}
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
$reflProperty = $refl->getProperty($field);
$reflProperty->setAccessible(true);
if ($metadata->hasAssociation($field)) {
$target = $metadata->getAssociationTargetClass($field);
if ($metadata->isSingleValuedAssociation($field)) {
$value = $this->toOne($target, $this->hydrateValue($field, $value, $data));
$reflProperty->setValue($object, $value);
} elseif ($metadata->isCollectionValuedAssociation($field)) {
$this->toMany($object, $field, $target, $value);
}
} else {
$reflProperty->setValue($object, $this->hydrateValue($field, $value, $data));
}
}
return $object;
}
/**
* This function tries, given an array of data, to convert it to an object if the given array contains
* an identifier for the object. This is useful in a context of updating existing entities, without ugly
* tricks like setting manually the existing id directly into the entity
*
* @param array $data The data that may contain identifiers keys
* @param object $object
* @return object
*/
protected function tryConvertArrayToObject($data, $object)
{
$metadata = $this->metadata;
$identifierNames = $metadata->getIdentifierFieldNames($object);
$identifierValues = [];
if (empty($identifierNames)) {
return $object;
}
foreach ($identifierNames as $identifierName) {
if (!isset($data[$identifierName])) {
return $object;
}
$identifierValues[$identifierName] = $data[$identifierName];
}
return $this->find($identifierValues, $metadata->getName());
}
/**
* Handle ToOne associations
*
* When $value is an array but is not the $target's identifiers, $value is
* most likely an array of fieldset data. The identifiers will be determined
* and a target instance will be initialized and then hydrated. The hydrated
* target will be returned.
*
* @param string $target
* @param mixed $value
* @return object
*/
protected function toOne($target, $value)
{
$metadata = $this->objectManager->getClassMetadata($target);
if (is_array($value) && array_keys($value) != $metadata->getIdentifier()) {
// $value is most likely an array of fieldset data
$identifiers = array_intersect_key(
$value,
array_flip($metadata->getIdentifier())
);
$object = $this->find($identifiers, $target) ?: new $target;
return $this->hydrate($value, $object);
}
return $this->find($value, $target);
}
/**
* Handle ToMany associations. In proper Doctrine design, Collections should not be swapped, so
* collections are always handled by reference. Internally, every collection is handled using specials
* strategies that inherit from AbstractCollectionStrategy class, and that add or remove elements but without
* changing the collection of the object
*
* @param object $object
* @param mixed $collectionName
* @param string $target
* @param mixed $values
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function toMany($object, $collectionName, $target, $values)
{
$metadata = $this->objectManager->getClassMetadata(ltrim($target, '\\'));
$identifier = $metadata->getIdentifier();
if (!is_array($values) && !$values instanceof Traversable) {
$values = (array)$values;
}
$collection = [];
// If the collection contains identifiers, fetch the objects from database
foreach ($values as $value) {
if ($value instanceof $target) {
// assumes modifications have already taken place in object
$collection[] = $value;
continue;
} elseif (empty($value)) {
// assumes no id and retrieves new $target
$collection[] = $this->find($value, $target);
continue;
}
$find = [];
if (is_array($identifier)) {
foreach ($identifier as $field) {
switch (gettype($value)) {
case 'object':
$getter = 'get' . ucfirst($field);
if (method_exists($value, $getter)) {
$find[$field] = $value->$getter();
} elseif (property_exists($value, $field)) {
$find[$field] = $value->$field;
}
break;
case 'array':
if (array_key_exists($field, $value) && $value[$field] != null) {
$find[$field] = $value[$field];
unset($value[$field]); // removed identifier from persistable data
}
break;
default:
$find[$field] = $value;
break;
}
}
}
if (!empty($find) && $found = $this->find($find, $target)) {
$collection[] = (is_array($value)) ? $this->hydrate($value, $found) : $found;
} else {
$collection[] = (is_array($value)) ? $this->hydrate($value, new $target) : new $target;
}
}
$collection = array_filter(
$collection,
function ($item) {
return null !== $item;
}
);
// Set the object so that the strategy can extract the Collection from it
/** @var \DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy $collectionStrategy */
$collectionStrategy = $this->getStrategy($collectionName);
$collectionStrategy->setObject($object);
// We could directly call hydrate method from the strategy, but if people want to override
// hydrateValue function, they can do it and do their own stuff
$this->hydrateValue($collectionName, $collection, $values);
}
/**
* Handle various type conversions that should be supported natively by Doctrine (like DateTime)
*
* @param mixed $value
* @param string $typeOfField
* @return DateTime
*/
protected function handleTypeConversions($value, $typeOfField)
{
switch ($typeOfField) {
case 'datetimetz':
case 'datetime':
case 'time':
case 'date':
if ('' === $value) {
return null;
}
if (is_int($value)) {
$dateTime = new DateTime();
$dateTime->setTimestamp($value);
$value = $dateTime;
} elseif (is_string($value)) {
$value = new DateTime($value);
}
break;
default:
}
return $value;
}
/**
* Find an object by a given target class and identifier
*
* @param mixed $identifiers
* @param string $targetClass
*
* @return object|null
*/
protected function find($identifiers, $targetClass)
{
if ($identifiers instanceof $targetClass) {
return $identifiers;
}
if ($this->isNullIdentifier($identifiers)) {
return null;
}
return $this->objectManager->find($targetClass, $identifiers);
}
/**
* Verifies if a provided identifier is to be considered null
*
* @param mixed $identifier
*
* @return bool
*/
private function isNullIdentifier($identifier)
{
if (null === $identifier) {
return true;
}
if ($identifier instanceof Traversable || is_array($identifier)) {
$nonNullIdentifiers = array_filter(
ArrayUtils::iteratorToArray($identifier),
function ($value) {
return null !== $value;
}
);
return empty($nonNullIdentifiers);
}
return false;
}
/**
* Applies the naming strategy if there is one set
*
* @param string $field
*
* @return string
*/
protected function computeHydrateFieldName($field)
{
if ($this->hasNamingStrategy()) {
$field = $this->getNamingStrategy()->hydrate($field);
}
return $field;
}
/**
* Applies the naming strategy if there is one set
*
* @param string $field
*
* @return string
*/
protected function computeExtractFieldName($field)
{
if ($this->hasNamingStrategy()) {
$field = $this->getNamingStrategy()->extract($field);
}
return $field;
}
}

View File

@@ -1,15 +0,0 @@
<?php
namespace App\Hydrator;
use Interop\Container\ContainerInterface;
class DoctrineObjectFactory
{
public function __invoke(ContainerInterface $container)
{
$em = $container->get('doctrine.entity_manager.orm_default');
return new DoctrineObject($em);
}
}

View File

@@ -1,66 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator\Filter;
use Zend\Hydrator\Filter\FilterInterface;
/**
* Provides a filter to restrict returned fields by whitelisting or
* blacklisting property names.
*
* @license MIT
* @link http://www.doctrine-project.org/
* @author Liam O'Boyle <liam@ontheroad.net.nz>
*/
class PropertyName implements FilterInterface
{
/**
* The propteries to exclude.
*
* @var array
*/
protected $properties = [];
/**
* Either an exclude or an include.
*
* @var bool
*/
protected $exclude = null;
/**
* @param [ string | array ] $properties The properties to exclude or include.
* @param bool $exclude If the method should be excluded
*/
public function __construct($properties, $exclude = true)
{
$this->exclude = $exclude;
$this->properties = is_array($properties)
? $properties
: [$properties];
}
public function filter($property)
{
return in_array($property, $this->properties)
? !$this->exclude
: $this->exclude;
}
}

View File

@@ -1,190 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator\Strategy;
use InvalidArgumentException;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Zend\Hydrator\Strategy\StrategyInterface;
/**
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
abstract class AbstractCollectionStrategy implements StrategyInterface
{
/**
* @var string
*/
protected $collectionName;
/**
* @var ClassMetadata
*/
protected $metadata;
/**
* @var object
*/
protected $object;
/**
* Set the name of the collection
*
* @param string $collectionName
* @return AbstractCollectionStrategy
*/
public function setCollectionName($collectionName)
{
$this->collectionName = (string) $collectionName;
return $this;
}
/**
* Get the name of the collection
*
* @return string
*/
public function getCollectionName()
{
return $this->collectionName;
}
/**
* Set the class metadata
*
* @param ClassMetadata $classMetadata
* @return AbstractCollectionStrategy
*/
public function setClassMetadata(ClassMetadata $classMetadata)
{
$this->metadata = $classMetadata;
return $this;
}
/**
* Get the class metadata
*
* @return ClassMetadata
*/
public function getClassMetadata()
{
return $this->metadata;
}
/**
* Set the object
*
* @param object $object
*
* @throws \InvalidArgumentException
*
* @return AbstractCollectionStrategy
*/
public function setObject($object)
{
if (!is_object($object)) {
throw new InvalidArgumentException(
sprintf('The parameter given to setObject method of %s class is not an object', get_called_class())
);
}
$this->object = $object;
return $this;
}
/**
* Get the object
*
* @return object
*/
public function getObject()
{
return $this->object;
}
/**
* {@inheritDoc}
*/
public function extract($value)
{
return $value;
}
/**
* Return the collection by value (using the public API)
*
* @throws \InvalidArgumentException
*
* @return Collection
*/
protected function getCollectionFromObjectByValue()
{
$object = $this->getObject();
$getter = 'get' . ucfirst($this->getCollectionName());
if (!method_exists($object, $getter)) {
throw new InvalidArgumentException(
sprintf(
'The getter %s to access collection %s in object %s does not exist',
$getter,
$this->getCollectionName(),
get_class($object)
)
);
}
return $object->$getter();
}
/**
* Return the collection by reference (not using the public API)
*
* @return Collection
*/
protected function getCollectionFromObjectByReference()
{
$object = $this->getObject();
$refl = $this->getClassMetadata()->getReflectionClass();
$reflProperty = $refl->getProperty($this->getCollectionName());
$reflProperty->setAccessible(true);
return $reflProperty->getValue($object);
}
/**
* This method is used internally by array_udiff to check if two objects are equal, according to their
* SPL hash. This is needed because the native array_diff only compare strings
*
* @param object $a
* @param object $b
*
* @return int
*/
protected function compareObjects($a, $b)
{
return strcmp(spl_object_hash($a), spl_object_hash($b));
}
}

View File

@@ -1,58 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator\Strategy;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy remove elements from the original collection. For instance, if the
* collection initially contains elements A and B, and that the new collection contains elements B and C, then the
* final collection will contain elements B and C (while element A will be asked to be removed).
*
* This strategy is by reference, this means it won't use public API to add/remove elements to the collection
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class AllowRemoveByReference extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
*/
public function hydrate($value)
{
$collection = $this->getCollectionFromObjectByReference();
$collectionArray = $collection->toArray();
$toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
$toRemove = array_udiff($collectionArray, $value, [$this, 'compareObjects']);
foreach ($toAdd as $element) {
$collection->add($element);
}
foreach ($toRemove as $element) {
$collection->removeElement($element);
}
return $collection;
}
}

View File

@@ -1,76 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator\Strategy;
use Doctrine\Common\Collections\Collection;
use LogicException;
use Doctrine\Common\Collections\ArrayCollection;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy remove elements from the original collection. For instance, if the
* collection initially contains elements A and B, and that the new collection contains elements B and C, then the
* final collection will contain elements B and C (while element A will be asked to be removed).
*
* This strategy is by value, this means it will use the public API (in this case, adder and remover)
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class AllowRemoveByValue extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
*/
public function hydrate($value)
{
// AllowRemove strategy need "adder" and "remover"
$adder = 'add' . ucfirst($this->collectionName);
$remover = 'remove' . ucfirst($this->collectionName);
if (!method_exists($this->object, $adder) || !method_exists($this->object, $remover)) {
throw new LogicException(
sprintf(
'AllowRemove strategy for DoctrineModule hydrator requires both %s and %s to be defined in %s
entity domain code, but one or both seem to be missing',
$adder,
$remover,
get_class($this->object)
)
);
}
$collection = $this->getCollectionFromObjectByValue();
if ($collection instanceof Collection) {
$collection = $collection->toArray();
}
$toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects']));
$toRemove = new ArrayCollection(array_udiff($collection, $value, [$this, 'compareObjects']));
$this->object->$adder($toAdd);
$this->object->$remover($toRemove);
return $collection;
}
}

View File

@@ -1,53 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator\Strategy;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
* instance, if the collection initially contains elements A and B, and that the new collection contains elements B
* and C, then the final collection will contain elements A, B and C.
*
* This strategy is by reference, this means it won't use the public API to remove elements
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class DisallowRemoveByReference extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
*/
public function hydrate($value)
{
$collection = $this->getCollectionFromObjectByReference();
$collectionArray = $collection->toArray();
$toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
foreach ($toAdd as $element) {
$collection->add($element);
}
return $collection;
}
}

View File

@@ -1,72 +0,0 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace App\Hydrator\Strategy;
use Doctrine\Common\Collections\Collection;
use LogicException;
use Doctrine\Common\Collections\ArrayCollection;
/**
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
* the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
* instance, if the collection initially contains elements A and B, and that the new collection contains elements B
* and C, then the final collection will contain elements A, B and C.
*
* This strategy is by value, this means it will use the public API (in this case, remover)
*
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.7.0
* @author Michael Gallego <mic.gallego@gmail.com>
*/
class DisallowRemoveByValue extends AbstractCollectionStrategy
{
/**
* {@inheritDoc}
*/
public function hydrate($value)
{
// AllowRemove strategy need "adder"
$adder = 'add' . ucfirst($this->collectionName);
if (!method_exists($this->object, $adder)) {
throw new LogicException(
sprintf(
'DisallowRemove strategy for DoctrineModule hydrator requires %s to
be defined in %s entity domain code, but it seems to be missing',
$adder,
get_class($this->object)
)
);
}
$collection = $this->getCollectionFromObjectByValue();
if ($collection instanceof Collection) {
$collection = $collection->toArray();
}
$toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects']));
$this->object->$adder($toAdd);
return $collection;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Log;
use Interop\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
use Zend\Stratigility\Middleware\ErrorHandler;
class ExceptionHandlerListenerDelegatorFactory implements DelegatorFactoryInterface
{
/**
* @param ContainerInterface $container
* @param string $name
* @param callable $callback
* @param array|null $options
* @return object|ErrorHandler
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
{
if (! $container->has(LoggerInterface::class)) {
throw new \RuntimeException(
"You must define a factory for LoggerInterface. Check loslog.global.php.dist for an example."
);
}
$logger = $container->get(LoggerInterface::class);
/* @var ErrorHandler $errorHandler */
$errorHandler = $callback();
$errorHandler->attachListener(new LosLogExceptionListener($logger));
return $errorHandler;
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Log;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Log\LoggerInterface;
class LosLogExceptionListener
{
const LOG_FORMAT = "%d [%s] %s: %s. File: %s:%s\n%s";
private $logger;
/**
* LosLogListener constructor.
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @param \Throwable $error
* @param Request $request
* @param Response $response
*/
public function __invoke($error, Request $request, Response $response)
{
$this->logger->error(sprintf(
self::LOG_FORMAT,
$response->getStatusCode(),
$request->getMethod(),
(string) $request->getUri(),
$error->getMessage(),
$error->getFile(),
$error->getLine(),
$error->getTraceAsString()
));
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Middleware;
use App\Service\KoinService;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface;

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Response;
use Zend\Diactoros\Response\JsonResponse;

View File

@@ -1,10 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\Sms;
use Doctrine\ORM\EntityManager;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Zend\EventManager\Event;
@@ -28,48 +29,43 @@ class KoinService
*/
const OTP_SMS_PATTERN = '#([0-9]{6}) ([0-9]{1,2}:[0-9]{1,2}) K[áà]rty[áà]s v[áà]s[áà]rl[áà]s/z[áà]rol[áà]s: -([0-9,.]+) ([A-Z]{2,3}); (.*?); K[áà]rtyasz[áà]m: ...[0-9]{4}; Egyenleg: \+[0-9.]+ HUF - OTPdirekt#msiu';
/**
* @var array
*/
private $config;
/**
* @var Client
*/
/** @var Client */
private $httpClient;
/**
* @var string
*/
/** @var array */
private $config;
/** @var EntityManager */
private $entityManager;
/** @var string */
private $accountId;
/**
* @var string
*/
/** @var string */
private $csrfParam;
/**
* @var string
*/
/** @var string */
private $csrfToken;
/**
* @var array
*/
/** @var array */
private $expenseCategories = [];
/**
* @var array
*/
/** @var array */
private $incomeCategories = [];
public function __construct(Client $httpClient, array $config)
public function __construct(Client $httpClient, array $config, EntityManager $em)
{
$this->config = $config;
$this->httpClient = $httpClient;
$this->entityManager = $em;
}
/**
* @param Event $event
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onReceiveSmsListener(Event $event)
{
$this->onReceiveSms($event->getParam('sms'));
@@ -78,20 +74,30 @@ class KoinService
/**
* @param Sms $sms
* @return int
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onReceiveSms(Sms $sms)
{
$setIsHandledFlag = true;
if (1 === ($matchStatus = preg_match(self::OTP_SMS_PATTERN, $sms->getText(), $otpMatches))) {
$datePart = implode("-", sscanf($otpMatches[1], '%2c%2c%2c'));
$this->saveTransaction(
$result = $this->saveTransaction(
str_replace(",", ".", str_replace(".", "", $otpMatches[3])),
$otpMatches[4],
"20${datePart}",
$this->getExpenseCategory($otpMatches[5]),
$this->getExpenseTags($otpMatches[5])
);
if ( 200 > $result || 299 < $result) {
$setIsHandledFlag = false;
}
}
$sms->setParsedAndHandled($setIsHandledFlag);
$this->entityManager->flush();
return $matchStatus;
}
@@ -133,6 +139,18 @@ class KoinService
return 'Étel';
}
if (false !== strpos($posInfo, "HAI NAM BISTRÓ")) {
return 'Étel';
}
if (false !== strpos($posInfo, "FIRPO BURGER")) {
return 'Étel';
}
if (false !== strpos($posInfo, "VENDIT AUTOMATÁK")) {
return 'Étel';
}
if (false !== strpos($posInfo, "SZLOVàK ABC")) {
return 'Bevásárlás';
}
@@ -173,6 +191,14 @@ class KoinService
return 'Közművek';
}
if (false !== strpos($posInfo, "FUNDAMENTA")) {
return 'Megtakarítás';
}
if (false !== strpos($posInfo, "STEAMGAMES")) {
return 'Szórakozás';
}
if (false !== strpos($posInfo, "Aqua Electromax")) {
return 'Szórakozás';
}
@@ -181,6 +207,10 @@ class KoinService
return 'Sport';
}
if (false !== strpos($posInfo, "JYSK")) {
return 'Háztartás';
}
if (false !== strpos($posInfo, "MOL TÖLTÖàLL")) {
return 'Autó';
}
@@ -226,8 +256,20 @@ class KoinService
} elseif (false !== strpos($posInfo, "BKK AUTOMATA")) {
$tags[] = "Bérlet";
$tags[] = "BKV";
} elseif (false !== strpos($posInfo, "BKK-AUTOMATA")) {
$tags[] = "Bérlet";
$tags[] = "BKV";
} elseif (false !== strpos($posInfo, "SPAR")) {
$tags[] = 'Spar';
} elseif (false !== strpos($posInfo, "HAI NAM BISTRÓ")) {
$tags[] = 'Hai Nam Bistro';
$tags[] = 'Vietnámi';
} elseif (false !== strpos($posInfo, "FIRPO BURGER")) {
$tags[] = 'Firpo Burger';
$tags[] = 'Burger';
} elseif (false !== strpos($posInfo, "VENDIT AUTOMATÁK")) {
$tags[] = 'Junk food';
$tags[] = 'Automata';
} elseif (false !== strpos($posInfo, "TESCO")) {
$tags[] = 'Tesco';
} elseif (false !== strpos($posInfo, "SZLOVàK ABC")) {
@@ -256,10 +298,17 @@ class KoinService
$tags[] = 'Google play';
} elseif (false !== strpos($posInfo, "DIGI ")) {
$tags[] = 'Digi';
} elseif (false !== strpos($posInfo, "FUNDAMENTA")) {
$tags[] = 'Fundamenta';
} elseif (false !== strpos($posInfo, "STEAMGAMES")) {
$tags[] = 'Steam';
$tags[] = 'Gáma';
} elseif (false !== strpos($posInfo, "Aqua Electromax")) {
$tags[] = 'Aqua';
} elseif (false !== strpos($posInfo, "DECATHLON")) {
$tags[] = 'Decathlon';
} elseif (false !== strpos($posInfo, "JYSK")) {
$tags[] = 'Jysk';
} elseif (false !== strpos($posInfo, "MOL TÖLTÖàLL")) {
$tags[] = 'MOL';
$tags[] = 'Benzinkút';
@@ -286,14 +335,14 @@ class KoinService
* @return int
*/
public function saveTransaction(string $amount,
string $currency = 'HUF',
string $currency,
string $date,
string $category,
array $tags): int
{
$pageData = $this->login($this->config['koin.user'], $this->config['koin.pass']);
$this->loadFormData($pageData->getBody());
$this->loadFormData($pageData->getBody()->getContents());
return $this->postData(
$amount,
$currency,
@@ -314,7 +363,7 @@ class KoinService
$body = $httpResponse->getBody();
$this->getCsrfToken($body);
$this->getCsrfToken($body->getContents());
$httpResponse = $this->httpClient
->post(self::BASE_URI . "/site/login", [
@@ -336,9 +385,7 @@ class KoinService
$documentXpath = $this->getCsrfToken($htmlData);
$this->getAccountId($documentXpath);
/** @var \DOMNodeList $expenseOptionElements */
$expenseOptionElements = $documentXpath->query('//select[@id="transaction-category_id"]/optgroup[@label="Kiadás"]/option');
/** @var \DOMNodeList $incomeOptionElements */
$incomeOptionElements = $documentXpath->query('//select[@id="transaction-category_id"]/optgroup[@label="Bevétel"]/option');
/** @var \DOMElement $element */
@@ -367,13 +414,15 @@ class KoinService
libxml_use_internal_errors($xmlErrorHandling);
$documentXpath = new \DOMXPath($domDocument);
/** @var \DOMElement $paramElement */
$paramElement = $documentXpath->query('//meta[@name="csrf-param"]')->item(0);
/** @var \DOMElement $tokenElement */
$tokenElement = $documentXpath->query('//meta[@name="csrf-token"]')->item(0);
$this->csrfParam = $paramElement->getAttribute("content");
$this->csrfToken = $tokenElement->getAttribute("content");
if ($paramElement instanceof \DOMElement) {
$this->csrfParam = $paramElement->getAttribute("content");
}
if ($tokenElement instanceof \DOMElement) {
$this->csrfToken = $tokenElement->getAttribute("content");
}
return $documentXpath;
}
@@ -384,9 +433,10 @@ class KoinService
*/
private function getAccountId(\DOMXPath $documentXpath)
{
/** @var \DOMElement $accountIdElement */
$accountIdElement = $documentXpath->query('//input[@id="transaction-account_id"]')->item(0);
$this->accountId = $accountIdElement->getAttribute("value");
if ($accountIdElement instanceof \DOMElement) {
$this->accountId = $accountIdElement->getAttribute("value");
}
}
/**
@@ -397,7 +447,7 @@ class KoinService
* @param string $tags
* @return int
*/
private function postData(string $amount, string $currency = 'HUF', string $date, string $category, string $tags): int
private function postData(string $amount, string $currency, string $date, string $category, string $tags): int
{
$saveResponse = $this->httpClient
->post(self::BASE_URI . "/main/save-transaction", [

View File

@@ -1,22 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Service;
use GuzzleHttp\Client;
use Interop\Container\ContainerInterface;
class KoinServiceFactory
{
public function __invoke(ContainerInterface $container)
{
$httpClient = new Client([
'cookies' => true,
]);
$config = $container->get('config');
return new KoinService($httpClient, $config);
}
/**
* @param ContainerInterface $container
* @return KoinService
*/
public function __invoke(ContainerInterface $container)
{
$httpClient = new Client([
'cookies' => true,
]);
$config = $container->get('config');
$em = $container->get('doctrine.entity_manager.orm_default');
return new KoinService($httpClient, $config, $em);
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\SZEPCardEntry;
@@ -114,6 +116,7 @@ class SZEPManagerService
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
* @throws \Exception
*/
private function parseResult(string $resultXml)
{
@@ -140,7 +143,7 @@ class SZEPManagerService
$szepCardEntity = new SZEPCardEntry();
$szepCardEntity->setHash($hash)
->setAmount($amount)
->setAmount(intval($amount))
->setMerchant($merchant)
->setPocket($pocket)
->setDate(new \DateTimeImmutable(str_replace(".", "-", $date)));
@@ -150,7 +153,7 @@ class SZEPManagerService
/** @var SZEPCardEntry $newRecord */
foreach ($newRecords as $newRecord) {
$resultCode = $this->koinService->saveTransaction(
abs($newRecord->getAmount()),
(string)abs($newRecord->getAmount()),
'HUF',
$newRecord->getDate()->format("Y-m-d"),
$this->getCategory($newRecord),
@@ -202,6 +205,8 @@ class SZEPManagerService
$tags[] = 'Sushi';
} elseif (false !== strpos($SZEPCardEntry->getMerchant(), "Szentmihályi Uszoda")) {
$tags[] = 'Tope';
} elseif (false !== strpos($SZEPCardEntry->getMerchant(), "BME Sportközpont")) {
$tags[] = 'BME Sportközpont';
}
switch ($SZEPCardEntry->getPocket()) {
@@ -222,8 +227,8 @@ class SZEPManagerService
*/
private function encryptPayload(string $payload, $key = self::AES_KEY, $iv = self::IV_PARAM): string
{
$aesKeyString = call_user_func_array("pack", array_merge(array("c*"), $key));
$ivParamStr = call_user_func_array("pack", array_merge(array("c*"), $iv));
$aesKeyString = call_user_func_array("pack", array_merge(["c*"], $key));
$ivParamStr = call_user_func_array("pack", array_merge(["c*"], $iv));
return openssl_encrypt($payload, "AES-256-CBC", $aesKeyString, 0, $ivParamStr);
}

View File

@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManager; // @phan-suppress-current-line PhanUnreferencedUseNormal
use GuzzleHttp\Client;
use Interop\Container\ContainerInterface;

View File

@@ -1,9 +1,9 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\Sms;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
@@ -11,37 +11,46 @@ use Zend\EventManager\EventManager;
class SmsStoreService
{
/**
* @var EntityManager
*/
private $em;
/** @var EntityManager */
private $em;
public function __construct(EntityManager $em, EventManager $eventManager)
{
$this->em = $em;
$this->eventManager = $eventManager;
}
/** @var EventManager */
private $eventManager;
public function storeSms(string $hashKey, int $direction, array $requestData): bool
{
$normalizedDate = str_replace("at ", "", $requestData['when']);
$user = $this->ensureUserExists($hashKey);
$sms = new Sms();
$sms->setDirection($direction)
->setContactName($requestData['contactName'])
->setContactNumber($requestData['contactNumber'])
->setOccuredAt(new \DateTime($normalizedDate))
->setOwner($user)
->setText($requestData['text']);
$this->em->persist($sms);
$this->em->flush();
public function __construct(EntityManager $em, EventManager $eventManager)
{
$this->em = $em;
$this->eventManager = $eventManager;
}
$this->eventManager->trigger('store.sms.persisted', $this, [
'sms' => $sms,
]);
/**
* @param string $hashKey
* @param int $direction
* @param array $requestData
* @return bool
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function storeSms(string $hashKey, int $direction, array $requestData): bool
{
$normalizedDate = str_replace("at ", "", $requestData['when']);
$user = $this->ensureUserExists($hashKey);
$sms = new Sms();
$sms->setDirection($direction)
->setContactName($requestData['contactName'])
->setContactNumber($requestData['contactNumber'])
->setOccuredAt(new \DateTime($normalizedDate))
->setOwner($user)
->setText($requestData['text']);
$this->em->persist($sms);
$this->em->flush();
return true;
}
$this->eventManager->trigger('store.sms.persisted', $this, [
'sms' => $sms,
]);
return true;
}
/**
* @param string $hashKey
@@ -49,20 +58,20 @@ class SmsStoreService
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
private function ensureUserExists(string $hashKey): User
{
/** @var User $user */
$user = $this->em->getRepository(User::class)->findOneBy([
'hashKey' => $hashKey
]);
private function ensureUserExists(string $hashKey): User
{
/** @var User $user */
$user = $this->em->getRepository(User::class)->findOneBy([
'hashKey' => $hashKey
]);
if($user === null) {
$user = new User();
$user->setHashKey($hashKey)->setName("Unknown");
$this->em->persist($user);
$this->em->flush();
if ($user === null) {
$user = new User();
$user->setHashKey($hashKey)->setName("Unknown");
$this->em->persist($user);
$this->em->flush();
}
return $user;
}
return $user;
}
}

View File

@@ -1,13 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManager;
class SmsStoreServiceFactory
{