Skip to content

Commit 3407f75

Browse files
committed
initial commit
0 parents  commit 3407f75

File tree

23 files changed

+649
-0
lines changed

23 files changed

+649
-0
lines changed

.github/workflows/integration.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Integration Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths-ignore:
8+
- "docs/**"
9+
- "*.md"
10+
pull_request:
11+
branches:
12+
- main
13+
paths-ignore:
14+
- "docs/**"
15+
- "*.md"
16+
17+
jobs:
18+
compute_matrix:
19+
runs-on: ubuntu-latest
20+
outputs:
21+
matrix: ${{ steps.supported-version.outputs.matrix }}
22+
steps:
23+
- uses: actions/checkout@v2
24+
- uses: graycoreio/github-actions-magento2/supported-version@main
25+
id: supported-version
26+
- run: echo ${{ steps.supported-version.outputs.matrix }}
27+
integration-workflow:
28+
needs: compute_matrix
29+
uses: graycoreio/github-actions-magento2/.github/workflows/integration.yaml@main
30+
with:
31+
package_name: graycore/magento2-graphql-logger
32+
matrix: ${{ needs.compute_matrix.outputs.matrix }}
33+
test_command: ../../../vendor/bin/phpunit ../../../vendor/graycore/magento2-graphql-logger/Test/Integration
34+
secrets:
35+
composer_auth: ${{ secrets.COMPOSER_AUTH }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
/composer.lock
3+
4+
/node_modules

Api/Data/LogInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Api\Data;
4+
5+
interface LogInterface
6+
{
7+
public const LOG_ID = 'log_id';
8+
public const HASH = 'hash';
9+
public const METHOD = 'method';
10+
public const QUERY = 'query';
11+
public const UPDATED = 'updated';
12+
13+
public function setLogId(int $logId): self;
14+
public function getLogId(): ?int;
15+
public function setHash(string $hash): self;
16+
public function getHash(): string;
17+
public function setMethod(string $method): self;
18+
public function getMethod(): string;
19+
public function setQuery(string $query): self;
20+
public function getQuery(): string;
21+
public function setUpdated(string $updated): self;
22+
public function getUpdated(): string;
23+
}

Api/LogRepositoryInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Api;
4+
5+
use Graycore\GraphQlLogger\Api\Data\LogInterface;
6+
7+
interface LogRepositoryInterface
8+
{
9+
public function save(LogInterface $log): LogInterface;
10+
public function getByHash(string $hash): LogInterface;
11+
}

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2022 Graycore, LLC
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Model/Config.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Model;
4+
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
7+
class Config
8+
{
9+
private const CONFIG_PATH_GRAPHQL_LOGGER_ENABLED = 'graphql/logger/enabled';
10+
11+
private ScopeConfigInterface $scopeConfig;
12+
13+
public function __construct(ScopeConfigInterface $scopeConfig)
14+
{
15+
$this->scopeConfig = $scopeConfig;
16+
}
17+
18+
public function isEnabled(): bool
19+
{
20+
return (bool) $this->scopeConfig->getValue(self::CONFIG_PATH_GRAPHQL_LOGGER_ENABLED);
21+
}
22+
}

Model/Log.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Model;
4+
5+
6+
use Graycore\GraphQlLogger\Api\Data\LogInterface;
7+
use Graycore\GraphQlLogger\Model\ResourceModel\Log as LogResource;
8+
use Magento\Framework\Model\AbstractModel;
9+
10+
class Log extends AbstractModel implements LogInterface
11+
{
12+
protected function _construct()
13+
{
14+
$this->_init(LogResource::class);
15+
}
16+
17+
public function setLogId(int $logId): LogInterface
18+
{
19+
$this->setData(self::LOG_ID, $logId);
20+
return $this;
21+
}
22+
23+
public function getLogId(): ?int
24+
{
25+
$logId = $this->getData(self::LOG_ID);
26+
return $logId !== null ? (int) $logId : null;
27+
}
28+
29+
public function setHash(string $hash): LogInterface
30+
{
31+
$this->setData(self::HASH, $hash);
32+
return $this;
33+
}
34+
35+
public function getHash(): string
36+
{
37+
return $this->getData(self::HASH);
38+
}
39+
40+
public function setMethod(string $method): LogInterface
41+
{
42+
$this->setData(self::METHOD, $method);
43+
return $this;
44+
}
45+
46+
public function getMethod(): string
47+
{
48+
return $this->getData(self::METHOD);
49+
}
50+
51+
public function setQuery(string $query): LogInterface
52+
{
53+
$this->setData(self::QUERY, $query);
54+
return $this;
55+
}
56+
57+
public function getQuery(): string
58+
{
59+
return $this->getData(self::QUERY);
60+
}
61+
62+
public function setUpdated(string $updated): LogInterface
63+
{
64+
$this->setData(self::UPDATED, $updated);
65+
return $this;
66+
}
67+
68+
public function getUpdated(): string
69+
{
70+
return $this->getData(self::UPDATED);
71+
}
72+
}

Model/LogRepository.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Model;
4+
5+
use Graycore\GraphQlLogger\Api\Data\LogInterface;
6+
use Graycore\GraphQlLogger\Api\Data\LogInterfaceFactory as LogFactory;
7+
use Graycore\GraphQlLogger\Api\LogRepositoryInterface;
8+
use Graycore\GraphQlLogger\Model\ResourceModel\Log as LogResource;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Psr\Log\LoggerInterface;
11+
12+
class LogRepository implements LogRepositoryInterface
13+
{
14+
private LogFactory $logFactory;
15+
private LogResource $logResource;
16+
private LoggerInterface $logger;
17+
18+
public function __construct(LogFactory $logFactory, LogResource $logResource, LoggerInterface $logger)
19+
{
20+
$this->logFactory = $logFactory;
21+
$this->logResource = $logResource;
22+
$this->logger = $logger;
23+
}
24+
25+
public function save(LogInterface $log): LogInterface
26+
{
27+
try {
28+
$this->logResource->save($log);
29+
} catch (\Exception $e) {
30+
$this->logger->warning('Unable to log query.', ['message' => $e->getMessage()]);
31+
}
32+
return $log;
33+
}
34+
35+
public function getByHash(string $hash): LogInterface
36+
{
37+
/** @var LogInterface $log */
38+
$log = $this->logFactory->create();
39+
$this->logResource->load($log, $hash, LogInterface::HASH);
40+
if ($log->getLogId() === null) {
41+
throw new NoSuchEntityException(__('No log exists for specified hash.'));
42+
}
43+
return $log;
44+
}
45+
}

Model/ResourceModel/Log.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Model\ResourceModel;
4+
5+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
6+
7+
class Log extends AbstractDb
8+
{
9+
protected function _construct()
10+
{
11+
$this->_init('graycore_graphql_log', 'log_id');
12+
}
13+
}

Plugin/LoggerPlugin.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlLogger\Plugin;
4+
5+
use Graycore\GraphQlLogger\Api\LogRepositoryInterface;
6+
use Graycore\GraphQlLogger\Api\Data\LogInterfaceFactory as LogFactory;
7+
use Graycore\GraphQlLogger\Model\Config;
8+
use Magento\Framework\App\RequestInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\GraphQl\Query\QueryProcessor;
11+
use Magento\Framework\GraphQl\Schema;
12+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
13+
14+
class LoggerPlugin
15+
{
16+
private LogRepositoryInterface $logRepository;
17+
private LogFactory $logFactory;
18+
private RequestInterface $request;
19+
private TimezoneInterface $date;
20+
private Config $config;
21+
22+
public function __construct(
23+
RequestInterface $request,
24+
LogRepositoryInterface $logRepository,
25+
LogFactory $logFactory,
26+
TimezoneInterface $date,
27+
Config $config
28+
) {
29+
$this->request = $request;
30+
$this->logRepository = $logRepository;
31+
$this->logFactory = $logFactory;
32+
$this->date = $date;
33+
$this->config = $config;
34+
}
35+
36+
public function afterProcess(
37+
QueryProcessor $queryProcessor,
38+
array $value,
39+
Schema $schema,
40+
string $source
41+
): array {
42+
if (!$this->config->isEnabled()) {
43+
return $value;
44+
}
45+
46+
$hash = hash('sha256', $source);
47+
48+
try {
49+
$log = $this->logRepository->getByHash($hash);
50+
} catch (NoSuchEntityException $e) {
51+
$log = $this->logFactory->create();
52+
}
53+
54+
$log->setHash($hash)
55+
->setMethod($this->request->getMethod())
56+
->setQuery($source)
57+
->setUpdated($this->date->date()->format('Y-m-d H:i:s'));
58+
$this->logRepository->save($log);
59+
60+
return $value;
61+
}
62+
}

0 commit comments

Comments
 (0)