* debug cmd added

* auth fix
This commit is contained in:
Danyi Dávid 2019-05-13 22:31:46 +02:00
parent 7006443669
commit a40ac5fb70
8 changed files with 128 additions and 32 deletions

View File

@ -6,12 +6,14 @@ return [
'factories' => [ 'factories' => [
App\Command\KoinImportCommand::class => App\Command\KoinImportCommandFactory::class, App\Command\KoinImportCommand::class => App\Command\KoinImportCommandFactory::class,
App\Command\PeriodicSZEPCommand::class => App\Command\PeriodicSZEPCommandFactory::class, App\Command\PeriodicSZEPCommand::class => App\Command\PeriodicSZEPCommandFactory::class,
App\Command\DebugCommand::class => App\Command\DebugCommandFactory::class,
], ],
], ],
'console' => [ 'console' => [
'commands' => [ 'commands' => [
App\Command\KoinImportCommand::class, App\Command\KoinImportCommand::class,
App\Command\PeriodicSZEPCommand::class, App\Command\PeriodicSZEPCommand::class,
App\Command\DebugCommand::class,
], ],
], ],
]; ];

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Service\KoinService;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DebugCommand extends Command
{
/**
* @var KoinService
*/
private $koinService;
/**
* @var EntityManager
*/
private $em;
public function __construct(KoinService $koinService,
EntityManager $em)
{
$this->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());
}
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Service\KoinService;
use Interop\Container\ContainerInterface;
class DebugCommandFactory
{
public function __invoke(ContainerInterface $container)
{
/** @var KoinService $koinService */
$koinService = $container->get(KoinService::class);
$em = $container->get('doctrine.entity_manager.orm_default');
return new DebugCommand($koinService, $em);
}
}

View File

@ -7,6 +7,8 @@ namespace App\Command;
use App\Entity\Sms; use App\Entity\Sms;
use App\Service\KoinService; use App\Service\KoinService;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -48,8 +50,8 @@ class KoinImportCommand extends Command
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* @return int|null|void * @return int|null|void
* @throws \Doctrine\ORM\ORMException * @throws ORMException
* @throws \Doctrine\ORM\OptimisticLockException * @throws OptimisticLockException
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {

View File

@ -5,6 +5,9 @@ declare(strict_types=1);
namespace App\Command; namespace App\Command;
use App\Service\SZEPManagerService; 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\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -33,9 +36,9 @@ class PeriodicSZEPCommand extends Command
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* @return int|null|void * @return int|null|void
* @throws \Doctrine\ORM\ORMException * @throws ORMException
* @throws \Doctrine\ORM\OptimisticLockException * @throws OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException * @throws TransactionRequiredException
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {

View File

@ -341,7 +341,7 @@ class KoinService
array $tags): int array $tags): int
{ {
$pageData = $this->login($this->config['koin.user'], $this->config['koin.pass']); $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); $accounts = $this->getAccounts($token);
$categories = $this->getCategories($token); $categories = $this->getCategories($token);
@ -358,7 +358,7 @@ class KoinService
$saveResponse = $this->httpClient $saveResponse = $this->httpClient
->post(self::BASE_URI . "/web/transactions", [ ->post(self::BASE_URI . "/web/transactions", [
'headers' => ['Bearer' => $token], 'headers' => $this->authHeader($token),
'json' => [ 'json' => [
'account_id' => $accounts[0]['id'], 'account_id' => $accounts[0]['id'],
'category_id' => $categoryId, 'category_id' => $categoryId,
@ -373,6 +373,13 @@ class KoinService
return $saveResponse->getStatusCode(); 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 $user
* @param string $pass * @param string $pass
@ -396,9 +403,7 @@ class KoinService
private function getAccounts(string $token): array private function getAccounts(string $token): array
{ {
$response = $this->httpClient->get(self::BASE_URI . "/web/accounts", [ $response = $this->httpClient->get(self::BASE_URI . "/web/accounts", [
'headers' => [ 'headers' => $this->authHeader($token),
'Bearer' => $token,
],
]); ]);
return Json::decode($response->getBody(), Json::TYPE_ARRAY)['accounts']; return Json::decode($response->getBody(), Json::TYPE_ARRAY)['accounts'];
} }
@ -411,7 +416,7 @@ class KoinService
{ {
$response = $this->httpClient->get(self::BASE_URI . "/web/categories", [ $response = $this->httpClient->get(self::BASE_URI . "/web/categories", [
'query' => ['show_hidden' => 1], 'query' => ['show_hidden' => 1],
'headers' => ['Bearer' => $token], 'headers' => $this->authHeader($token),
]); ]);
return Json::decode($response->getBody(), Json::TYPE_ARRAY)['categories']; return Json::decode($response->getBody(), Json::TYPE_ARRAY)['categories'];
} }
@ -424,4 +429,10 @@ class KoinService
{ {
return implode(';', $tags); return implode(';', $tags);
} }
private function authHeader(string $token): array {
return [
'Authorization' => "Bearer ${token}",
];
}
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Service; namespace App\Service;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
class KoinServiceFactory class KoinServiceFactory
@ -17,7 +18,7 @@ class KoinServiceFactory
public function __invoke(ContainerInterface $container) public function __invoke(ContainerInterface $container)
{ {
$httpClient = new Client([ $httpClient = new Client([
'cookies' => true, RequestOptions::COOKIES => true,
]); ]);
$config = $container->get('config'); $config = $container->get('config');
$em = $container->get('doctrine.entity_manager.orm_default'); $em = $container->get('doctrine.entity_manager.orm_default');

View File

@ -5,7 +5,16 @@ declare(strict_types=1);
namespace App\Service; namespace App\Service;
use App\Entity\SZEPCardEntry; use App\Entity\SZEPCardEntry;
use DateInterval;
use DateTimeImmutable;
use Doctrine\ORM\EntityManager; 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; use GuzzleHttp\Client;
class SZEPManagerService class SZEPManagerService
@ -61,10 +70,10 @@ class SZEPManagerService
} }
/** /**
* @throws \Doctrine\ORM\ORMException * @throws ORMException
* @throws \Doctrine\ORM\OptimisticLockException * @throws OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException * @throws TransactionRequiredException
* @throws \Exception * @throws Exception
*/ */
public function pollRecent() public function pollRecent()
{ {
@ -75,7 +84,7 @@ class SZEPManagerService
/** /**
* @return string * @return string
* @throws \Exception * @throws Exception
*/ */
private function getRecentXml(): ?string 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.card'], $cryptCardId, $certificate, OPENSSL_PKCS1_PADDING);
openssl_public_encrypt($this->config['szep.key'], $cryptCardKey, $certificate, OPENSSL_PKCS1_PADDING); openssl_public_encrypt($this->config['szep.key'], $cryptCardKey, $certificate, OPENSSL_PKCS1_PADDING);
$endDate = new \DateTimeImmutable(); $endDate = new DateTimeImmutable();
$startDate = $endDate->sub(new \DateInterval('P7D')); $startDate = $endDate->sub(new DateInterval('P7D'));
$gueryTemplate = file_get_contents(self::TEMPLATE_QUERY_CARD); $gueryTemplate = file_get_contents(self::TEMPLATE_QUERY_CARD);
$query = sprintf($gueryTemplate, $query = sprintf($gueryTemplate,
@ -97,15 +106,15 @@ class SZEPManagerService
$soapXml = sprintf(file_get_contents(self::TEMPLATE_WORKFLOW), $this->encryptPayload($query)); $soapXml = sprintf(file_get_contents(self::TEMPLATE_WORKFLOW), $this->encryptPayload($query));
try{ try{
$soapResponseXml = $this->doSoapRequest($soapXml); $soapResponseXml = $this->doSoapRequest($soapXml);
} catch (\Exception $e) { } catch (Exception $e) {
return null; return null;
} }
$domDocument = new \DOMDocument(); $domDocument = new DOMDocument();
$domDocument->loadXML($soapResponseXml); $domDocument->loadXML($soapResponseXml);
$documentXpath = new \DOMXPath($domDocument); $documentXpath = new DOMXPath($domDocument);
/** @var \DOMElement $returnElement */ /** @var DOMElement $returnElement */
$returnElement = $documentXpath->query('//return/result')->item(0); $returnElement = $documentXpath->query('//return/result')->item(0);
return base64_decode($returnElement->textContent); return base64_decode($returnElement->textContent);
} }
@ -113,23 +122,23 @@ class SZEPManagerService
/** /**
* Parse the decoded payload * Parse the decoded payload
* @param string $resultXml * @param string $resultXml
* @throws \Doctrine\ORM\ORMException * @throws ORMException
* @throws \Doctrine\ORM\OptimisticLockException * @throws OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException * @throws TransactionRequiredException
* @throws \Exception * @throws Exception
*/ */
private function parseResult(string $resultXml) private function parseResult(string $resultXml)
{ {
$domDocument = new \DOMDocument(); $domDocument = new DOMDocument();
$domDocument->loadXML($resultXml); $domDocument->loadXML($resultXml);
$documentXpath = new \DOMXPath($domDocument); $documentXpath = new DOMXPath($domDocument);
/** @var \DOMElement[] $returnElements */ /** @var DOMElement[] $returnElements */
$recordElements = $documentXpath->query('/answer/resultset/record'); $recordElements = $documentXpath->query('/answer/resultset/record');
$newRecords = []; $newRecords = [];
/** @var \DOMElement $element */ /** @var DOMElement $element */
foreach ($recordElements as $element) { foreach ($recordElements as $element) {
$date = trim($documentXpath->query('./datum', $element)->item(0)->textContent); $date = trim($documentXpath->query('./datum', $element)->item(0)->textContent);
$amount = trim($documentXpath->query('./osszeg', $element)->item(0)->textContent); $amount = trim($documentXpath->query('./osszeg', $element)->item(0)->textContent);
@ -146,7 +155,7 @@ class SZEPManagerService
->setAmount(intval($amount)) ->setAmount(intval($amount))
->setMerchant($merchant) ->setMerchant($merchant)
->setPocket($pocket) ->setPocket($pocket)
->setDate(new \DateTimeImmutable(str_replace(".", "-", $date))); ->setDate(new DateTimeImmutable(str_replace(".", "-", $date)));
$newRecords[] = $szepCardEntity; $newRecords[] = $szepCardEntity;
} }