Passed
Pull Request — master (#16)
by
unknown
14:00
created

Page::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Aoe\Asdis\Domain\Model;
4
5
use Aoe\Asdis\Content\Replacement\Processor;
6
use Aoe\Asdis\Content\Scraper\ChainFactory;
7
use Aoe\Asdis\Domain\Model\Asset\Collection;
8
use Aoe\Asdis\Domain\Model\DistributionAlgorithm\Factory;
9
use Aoe\Asdis\Domain\Repository\ServerRepository;
10
use Aoe\Asdis\System\Configuration\Provider;
11
use Exception;
12
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
13
14
/**
15
 * Represents a page in the TYPO3 page tree.
16
 */
17
class Page
18
{
19
    private ?Collection $assets = null;
20
21
    private ?TypoScriptFrontendController $pageObject = null;
22
23
    private ?ChainFactory $scraperChainFactory = null;
24
25
    private ?Factory $distributionAlgorithmFactory = null;
26
27
    private ?ServerRepository $serverRepository = null;
28
29
    private ?Provider $configurationProvider = null;
30
31
    private ?Processor $replacementProcessor = null;
32
33
    public function __construct(Provider $configurationProvider) {
34
        $this->configurationProvider = $configurationProvider;
35
    }
36
37
    public function injectScraperChainFactory(ChainFactory $scraperChainFactory): void
38
    {
39
        $this->scraperChainFactory = $scraperChainFactory;
40
    }
41
42
    public function injectDistributionAlgorithmFactory(Factory $distributionAlgorithmFactory): void
43
    {
44
        $this->distributionAlgorithmFactory = $distributionAlgorithmFactory;
45
    }
46
47
    public function injectServerRepository(ServerRepository $serverRepository): void
48
    {
49
        $this->serverRepository = $serverRepository;
50
    }
51
52
/*
53
    public function injectConfigurationProvider(Provider $configurationProvider): void
54
    {
55
        $this->configurationProvider = $configurationProvider;
56
    }
57
    */
58
59
    public function injectReplacementProcessor(Processor $replacementProcessor): void
60
    {
61
        $this->replacementProcessor = $replacementProcessor;
62
    }
63
64
    /**
65
     * Scrapes the assets of the page. There is no replacement taking place. You have to call "replaceAssets" to replace
66
     * the paths after calling "scrapeAssets".
67
     */
68
    public function scrapeAssets(): void
69
    {
70
        if (!$this->configurationProvider->isReplacementEnabled()) {
0 ignored issues
show
Bug introduced by
The method isReplacementEnabled() does not exist on null. ( Ignorable by Annotation )

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

70
        if (!$this->configurationProvider->/** @scrutinizer ignore-call */ isReplacementEnabled()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            return;
72
        }
73
        $this->setAssets($this->scraperChainFactory->buildChain()->scrape($this->pageObject->content));
0 ignored issues
show
Bug introduced by
The method buildChain() does not exist on null. ( Ignorable by Annotation )

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

73
        $this->setAssets($this->scraperChainFactory->/** @scrutinizer ignore-call */ buildChain()->scrape($this->pageObject->content));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
    }
75
76
    /**
77
     * Replaces the assets of the page.
78 1
     * To force any replacement, you have to call "scrapeAssets" before.
79
     */
80 1
    public function replaceAssets(): void
81 1
    {
82
        if (!$this->configurationProvider->isReplacementEnabled()) {
83
            return;
84
        }
85
        $distributionAlgorithmKey = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $distributionAlgorithmKey is dead and can be removed.
Loading history...
86
        try {
87
            $distributionAlgorithmKey = $this->configurationProvider->getDistributionAlgorithmKey();
88
        } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
89
        }
90
        $distributionAlgorithm = $this->distributionAlgorithmFactory->buildDistributionAlgorithmFromKey($distributionAlgorithmKey);
0 ignored issues
show
Bug introduced by
The method buildDistributionAlgorithmFromKey() does not exist on null. ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-call */ 
91
        $distributionAlgorithm = $this->distributionAlgorithmFactory->buildDistributionAlgorithmFromKey($distributionAlgorithmKey);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        $distributionAlgorithm->distribute($this->assets, $this->serverRepository->findAllByPage($this));
0 ignored issues
show
Bug introduced by
It seems like $this->assets can also be of type null; however, parameter $assets of Aoe\Asdis\Domain\Model\D...Interface::distribute() does only seem to accept Aoe\Asdis\Domain\Model\Asset\Collection, 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

91
        $distributionAlgorithm->distribute(/** @scrutinizer ignore-type */ $this->assets, $this->serverRepository->findAllByPage($this));
Loading history...
Bug introduced by
The method findAllByPage() does not exist on null. ( Ignorable by Annotation )

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

91
        $distributionAlgorithm->distribute($this->assets, $this->serverRepository->/** @scrutinizer ignore-call */ findAllByPage($this));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
93
        $this->pageObject->content = $this->replacementProcessor->replace(
0 ignored issues
show
Bug introduced by
The method replace() does not exist on null. ( Ignorable by Annotation )

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

93
        /** @scrutinizer ignore-call */ 
94
        $this->pageObject->content = $this->replacementProcessor->replace(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
            $this->assets->getReplacementMap(),
0 ignored issues
show
Bug introduced by
The method getReplacementMap() does not exist on null. ( Ignorable by Annotation )

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

94
            $this->assets->/** @scrutinizer ignore-call */ 
95
                           getReplacementMap(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            $this->pageObject->content
96
        );
97 1
    }
98
99 1
    public function setAssets(Collection $assets): void
100 1
    {
101
        $this->assets = $assets;
102
    }
103
104
    public function setPageObject(TypoScriptFrontendController $pageObject): void
105
    {
106
        $this->pageObject = $pageObject;
107
    }
108
109
    public function getPageObject(): ?TypoScriptFrontendController
110
    {
111
        return $this->pageObject;
112
    }
113
114
    public function getAssets(): ?Collection
115
    {
116
        return $this->assets;
117
    }
118
}
119