diff --git a/config/autoload/cli.global.php b/config/autoload/cli.global.php index 362457c..511d388 100644 --- a/config/autoload/cli.global.php +++ b/config/autoload/cli.global.php @@ -6,12 +6,14 @@ return [ 'factories' => [ App\Command\KoinImportCommand::class => App\Command\KoinImportCommandFactory::class, App\Command\PeriodicSZEPCommand::class => App\Command\PeriodicSZEPCommandFactory::class, + App\Command\DebugCommand::class => App\Command\DebugCommandFactory::class, ], ], 'console' => [ 'commands' => [ App\Command\KoinImportCommand::class, App\Command\PeriodicSZEPCommand::class, + App\Command\DebugCommand::class, ], ], ]; diff --git a/src/App/Command/DebugCommand.php b/src/App/Command/DebugCommand.php new file mode 100644 index 0000000..a396dca --- /dev/null +++ b/src/App/Command/DebugCommand.php @@ -0,0 +1,49 @@ +koinService = $koinService; + $this->em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('debug') + ->setDescription('Nada'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int|null|void + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln($this->koinService->debug()); + } +} diff --git a/src/App/Command/DebugCommandFactory.php b/src/App/Command/DebugCommandFactory.php new file mode 100644 index 0000000..2a56c81 --- /dev/null +++ b/src/App/Command/DebugCommandFactory.php @@ -0,0 +1,19 @@ +get(KoinService::class); + $em = $container->get('doctrine.entity_manager.orm_default'); + return new DebugCommand($koinService, $em); + } +} diff --git a/src/App/Command/KoinImportCommand.php b/src/App/Command/KoinImportCommand.php index 25a6d57..5ab5f67 100644 --- a/src/App/Command/KoinImportCommand.php +++ b/src/App/Command/KoinImportCommand.php @@ -7,6 +7,8 @@ namespace App\Command; use App\Entity\Sms; use App\Service\KoinService; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\ORMException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -48,8 +50,8 @@ class KoinImportCommand extends Command * @param InputInterface $input * @param OutputInterface $output * @return int|null|void - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException + * @throws ORMException + * @throws OptimisticLockException */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/App/Command/PeriodicSZEPCommand.php b/src/App/Command/PeriodicSZEPCommand.php index eea9725..43d0077 100644 --- a/src/App/Command/PeriodicSZEPCommand.php +++ b/src/App/Command/PeriodicSZEPCommand.php @@ -5,6 +5,9 @@ declare(strict_types=1); namespace App\Command; use App\Service\SZEPManagerService; +use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\ORMException; +use Doctrine\ORM\TransactionRequiredException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -33,9 +36,9 @@ class PeriodicSZEPCommand extends Command * @param InputInterface $input * @param OutputInterface $output * @return int|null|void - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException - * @throws \Doctrine\ORM\TransactionRequiredException + * @throws ORMException + * @throws OptimisticLockException + * @throws TransactionRequiredException */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/App/Service/KoinService.php b/src/App/Service/KoinService.php index 4a04724..79f8db9 100644 --- a/src/App/Service/KoinService.php +++ b/src/App/Service/KoinService.php @@ -341,7 +341,7 @@ class KoinService array $tags): int { $pageData = $this->login($this->config['koin.user'], $this->config['koin.pass']); - $token = Json::decode($pageData->getBody())->token; + $token = Json::decode($pageData->getBody(), Json::TYPE_ARRAY)['token']; $accounts = $this->getAccounts($token); $categories = $this->getCategories($token); @@ -358,7 +358,7 @@ class KoinService $saveResponse = $this->httpClient ->post(self::BASE_URI . "/web/transactions", [ - 'headers' => ['Bearer' => $token], + 'headers' => $this->authHeader($token), 'json' => [ 'account_id' => $accounts[0]['id'], 'category_id' => $categoryId, @@ -373,6 +373,13 @@ class KoinService return $saveResponse->getStatusCode(); } + public function debug() + { + $pageData = $this->login($this->config['koin.user'], $this->config['koin.pass']); + $token = Json::decode($pageData->getBody(), Json::TYPE_ARRAY); + return $token; + } + /** * @param string $user * @param string $pass @@ -396,9 +403,7 @@ class KoinService private function getAccounts(string $token): array { $response = $this->httpClient->get(self::BASE_URI . "/web/accounts", [ - 'headers' => [ - 'Bearer' => $token, - ], + 'headers' => $this->authHeader($token), ]); return Json::decode($response->getBody(), Json::TYPE_ARRAY)['accounts']; } @@ -411,7 +416,7 @@ class KoinService { $response = $this->httpClient->get(self::BASE_URI . "/web/categories", [ 'query' => ['show_hidden' => 1], - 'headers' => ['Bearer' => $token], + 'headers' => $this->authHeader($token), ]); return Json::decode($response->getBody(), Json::TYPE_ARRAY)['categories']; } @@ -424,4 +429,10 @@ class KoinService { return implode(';', $tags); } + + private function authHeader(string $token): array { + return [ + 'Authorization' => "Bearer ${token}", + ]; + } } diff --git a/src/App/Service/KoinServiceFactory.php b/src/App/Service/KoinServiceFactory.php index d9d7d51..6397233 100644 --- a/src/App/Service/KoinServiceFactory.php +++ b/src/App/Service/KoinServiceFactory.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Service; use GuzzleHttp\Client; +use GuzzleHttp\RequestOptions; use Interop\Container\ContainerInterface; class KoinServiceFactory @@ -17,7 +18,7 @@ class KoinServiceFactory public function __invoke(ContainerInterface $container) { $httpClient = new Client([ - 'cookies' => true, + RequestOptions::COOKIES => true, ]); $config = $container->get('config'); $em = $container->get('doctrine.entity_manager.orm_default'); diff --git a/src/App/Service/SZEPManagerService.php b/src/App/Service/SZEPManagerService.php index 597b4a4..608d447 100644 --- a/src/App/Service/SZEPManagerService.php +++ b/src/App/Service/SZEPManagerService.php @@ -5,7 +5,16 @@ declare(strict_types=1); namespace App\Service; use App\Entity\SZEPCardEntry; +use DateInterval; +use DateTimeImmutable; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\ORMException; +use Doctrine\ORM\TransactionRequiredException; +use DOMDocument; +use DOMElement; +use DOMXPath; +use Exception; use GuzzleHttp\Client; class SZEPManagerService @@ -61,10 +70,10 @@ class SZEPManagerService } /** - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException - * @throws \Doctrine\ORM\TransactionRequiredException - * @throws \Exception + * @throws ORMException + * @throws OptimisticLockException + * @throws TransactionRequiredException + * @throws Exception */ public function pollRecent() { @@ -75,7 +84,7 @@ class SZEPManagerService /** * @return string - * @throws \Exception + * @throws Exception */ private function getRecentXml(): ?string { @@ -83,8 +92,8 @@ class SZEPManagerService openssl_public_encrypt($this->config['szep.card'], $cryptCardId, $certificate, OPENSSL_PKCS1_PADDING); openssl_public_encrypt($this->config['szep.key'], $cryptCardKey, $certificate, OPENSSL_PKCS1_PADDING); - $endDate = new \DateTimeImmutable(); - $startDate = $endDate->sub(new \DateInterval('P7D')); + $endDate = new DateTimeImmutable(); + $startDate = $endDate->sub(new DateInterval('P7D')); $gueryTemplate = file_get_contents(self::TEMPLATE_QUERY_CARD); $query = sprintf($gueryTemplate, @@ -97,15 +106,15 @@ class SZEPManagerService $soapXml = sprintf(file_get_contents(self::TEMPLATE_WORKFLOW), $this->encryptPayload($query)); try{ $soapResponseXml = $this->doSoapRequest($soapXml); - } catch (\Exception $e) { + } catch (Exception $e) { return null; } - $domDocument = new \DOMDocument(); + $domDocument = new DOMDocument(); $domDocument->loadXML($soapResponseXml); - $documentXpath = new \DOMXPath($domDocument); - /** @var \DOMElement $returnElement */ + $documentXpath = new DOMXPath($domDocument); + /** @var DOMElement $returnElement */ $returnElement = $documentXpath->query('//return/result')->item(0); return base64_decode($returnElement->textContent); } @@ -113,23 +122,23 @@ class SZEPManagerService /** * Parse the decoded payload * @param string $resultXml - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException - * @throws \Doctrine\ORM\TransactionRequiredException - * @throws \Exception + * @throws ORMException + * @throws OptimisticLockException + * @throws TransactionRequiredException + * @throws Exception */ private function parseResult(string $resultXml) { - $domDocument = new \DOMDocument(); + $domDocument = new DOMDocument(); $domDocument->loadXML($resultXml); - $documentXpath = new \DOMXPath($domDocument); - /** @var \DOMElement[] $returnElements */ + $documentXpath = new DOMXPath($domDocument); + /** @var DOMElement[] $returnElements */ $recordElements = $documentXpath->query('/answer/resultset/record'); $newRecords = []; - /** @var \DOMElement $element */ + /** @var DOMElement $element */ foreach ($recordElements as $element) { $date = trim($documentXpath->query('./datum', $element)->item(0)->textContent); $amount = trim($documentXpath->query('./osszeg', $element)->item(0)->textContent); @@ -146,7 +155,7 @@ class SZEPManagerService ->setAmount(intval($amount)) ->setMerchant($merchant) ->setPocket($pocket) - ->setDate(new \DateTimeImmutable(str_replace(".", "-", $date))); + ->setDate(new DateTimeImmutable(str_replace(".", "-", $date))); $newRecords[] = $szepCardEntity; }