Passed
Branch dev3 (d976d5)
by Dispositif
02:50
created

ParseTemplateHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 28
c 1
b 0
f 0
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 31 3
A __construct() 0 5 1
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Application\OuvrageComplete\Handlers;
12
13
14
use App\Application\InfrastructurePorts\DbAdapterInterface;
15
use App\Domain\Models\PageOuvrageDTO;
16
use App\Domain\Models\Wiki\OuvrageTemplate;
17
use App\Domain\Utils\TemplateParser;
18
use Psr\Log\LoggerInterface;
19
use Throwable;
20
21
class ParseTemplateHandler implements CompleteHandlerInterface
22
{
23
    /**
24
     * @var PageOuvrageDTO
25
     */
26
    protected $pageOuvrage;
27
    /**
28
     * @var LoggerInterface
29
     */
30
    protected $logger;
31
    /**
32
     * @var DbAdapterInterface
33
     */
34
    protected $queueAdapter;
35
36
    public function __construct(PageOuvrageDTO $pageOuvrage, DbAdapterInterface $queueAdapter, LoggerInterface $logger)
37
    {
38
        $this->pageOuvrage = $pageOuvrage;
39
        $this->logger = $logger;
40
        $this->queueAdapter = $queueAdapter;
41
    }
42
43
    public function handle(): ?OuvrageTemplate
44
    {
45
        try {
46
            $parse = TemplateParser::parseAllTemplateByName('ouvrage', $this->pageOuvrage->getRaw());
0 ignored issues
show
Bug introduced by
It seems like $this->pageOuvrage->getRaw() can also be of type null; however, parameter $text of App\Domain\Utils\Templat...arseAllTemplateByName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $parse = TemplateParser::parseAllTemplateByName('ouvrage', /** @scrutinizer ignore-type */ $this->pageOuvrage->getRaw());
Loading history...
47
            $origin = $parse['ouvrage'][0]['model'] ?? null;
48
        } catch (Throwable $e) {
49
            $this->logger->warning(sprintf(
50
                "*** ERREUR 432 impossible de transformer en modèle => skip %s : %s \n",
51
                $this->pageOuvrage->getId(),
52
                $this->pageOuvrage->getRaw()
53
            ));
54
            $this->queueAdapter->skipRow($this->pageOuvrage->getId());
55
            sleep(10);
56
57
            return null;
58
        }
59
60
        if (!$origin instanceof OuvrageTemplate) {
61
            $this->logger->warning(
62
                sprintf(
63
                    "*** ERREUR 433 impossible de transformer en modèle => skip %s : %s \n",
64
                    $this->pageOuvrage->getId(),
65
                    $this->pageOuvrage->getRaw()
66
                )
67
            );
68
            $this->queueAdapter->skipRow($this->pageOuvrage->getId());
69
            sleep(10);
70
            return null;
71
        }
72
73
        return $origin;
74
    }
75
}