* 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' => [
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,
],
],
];

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\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)
{

View File

@ -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)
{

View File

@ -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}",
];
}
}

View File

@ -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');

View File

@ -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;
}