|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Authors: Alex Gusev <[email protected]> |
| 4 | + * Since: 2020 |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Flancer32\Csp\Cron; |
| 8 | + |
| 9 | +/** |
| 10 | + * Analyze CSP violation reports and create policy rules. |
| 11 | + */ |
| 12 | +class Analyze |
| 13 | +{ |
| 14 | + /** @var \Flancer32\Csp\Helper\Config */ |
| 15 | + private $hlpCfg; |
| 16 | + /** @var \Psr\Log\LoggerInterface */ |
| 17 | + private $logger; |
| 18 | + /** @var \Magento\Framework\App\ResourceConnection */ |
| 19 | + private $resource; |
| 20 | + /** @var \Flancer32\Csp\Service\Report\Analyze */ |
| 21 | + private $srvAnalyze; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + \Psr\Log\LoggerInterface $logger, |
| 25 | + \Magento\Framework\App\ResourceConnection $resource, |
| 26 | + \Flancer32\Csp\Helper\Config $hlpCfg, |
| 27 | + \Flancer32\Csp\Service\Report\Analyze $srvAnalyze |
| 28 | + ) { |
| 29 | + $this->logger = $logger; |
| 30 | + $this->resource = $resource; |
| 31 | + $this->hlpCfg = $hlpCfg; |
| 32 | + $this->srvAnalyze = $srvAnalyze; |
| 33 | + } |
| 34 | + |
| 35 | + public function execute() |
| 36 | + { |
| 37 | + $enabled = $this->hlpCfg->getCronEnabled(); |
| 38 | + if ($enabled) { |
| 39 | + $conn = $this->resource->getConnection(); |
| 40 | + $conn->beginTransaction(); |
| 41 | + $req = new \Flancer32\Csp\Service\Report\Analyze\Request(); |
| 42 | + $resp = $this->srvAnalyze->execute($req); |
| 43 | + $conn->commit(); |
| 44 | + $added = $resp->getRulesAdded(); |
| 45 | + $deleted = $resp->getReportsDeleted(); |
| 46 | + $min = $resp->getReportsIdMin(); |
| 47 | + $max = $resp->getReportsIdMax(); |
| 48 | + if ($deleted) { |
| 49 | + $this->logger->debug("CSP analyze (cron): $added rules were added, $deleted reports were deleted (id: $min-$max)."); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments