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

ContentPostProcAll::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Aoe\Asdis\Middleware;
4
5
use Aoe\Asdis\Domain\Model\Asset\Collection;
6
use Aoe\Asdis\Domain\Model\Page;
7
use Aoe\Asdis\System\Configuration\Provider;
8
use Aoe\Asdis\System\Log\Logger;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use TYPO3\CMS\Core\Http\HtmlResponse;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
16
use Exception;
17
18
class ContentPostProcAll implements MiddlewareInterface
19
{
20
    private ?Page $page = null;
21
22
    private Provider $provider;
23
24
    private Logger $logger;
25
26
    public function __construct(Provider $provider, Logger $logger) {
27
        $this->provider = $provider;
28
        $this->logger = $logger;
29
    }
30
31
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33
        $response = $handler->handle($request);
34
35
        if (!$this->provider->isReplacementEnabled()) {
36
            return $response;
37
        }
38
        if ($this->provider->isDefaultHookHandlingDisabled()) {
39
            return $response;
40
        }
41
42
        try {
43
            $this->setPageObject($GLOBALS['TSFE']);
44
            $this->scrapeAndReplace();
45
            $response = new HtmlResponse($this->page->getPageObject()->content);
0 ignored issues
show
Bug introduced by
The method getPageObject() 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

45
            $response = new HtmlResponse($this->page->/** @scrutinizer ignore-call */ getPageObject()->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...
46
        } catch (Exception $exception) {
47
            $this->logger
48
                ->logException(__METHOD__, $exception);
49
        }
50
51
        return $response;
52
    }
53
54
    protected function scrapeAssets(): void
55
    {
56
        $this->page->scrapeAssets();
57
    }
58
59
    protected function replaceAssets(): void
60
    {
61
        $this->page->replaceAssets();
62
    }
63
64
    /**
65
     * Scrapes and replaces the assets of the current page.
66
     */
67
    protected function scrapeAndReplace(): void
68
    {
69
        $this->scrapeAssets();
70
        $this->replaceAssets();
71
    }
72
73
    protected function setPageObject(TypoScriptFrontendController $pObj): void
74
    {
75
        $page = GeneralUtility::makeInstance(Page::class);
76
        $page->setAssets(GeneralUtility::makeInstance(Collection::class));
77
        $page->setPageObject($pObj);
78
        $this->page = $page;
79
    }
80
}
81