Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageScannerService::uriExist()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
nc 9
nop 1
dl 0
loc 15
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
7
use Twig_Environment;
8
9
/**
10
 * Permit to find error in image or link.
11
 */
12
class PageScannerService
13
{
14
    protected $em;
15
    protected $pageHtml;
16
    protected $twig;
17
    protected $defaultTemplate;
18
    protected $currentPage;
19
    protected $webDir;
20
    protected $errors = [];
21
    protected $everChecked = [];
22
23
    public function __construct(
24
        Twig_Environment $twig,
25
        EntityManagerInterface $em,
26
        string $defaultTemplate,
27
        string $webDir
28
    ) {
29
        $this->twig = $twig;
30
        $this->em = $em;
31
        $this->defaultTemplate = $defaultTemplate;
32
        $this->webDir = $webDir;
33
    }
34
35
    public function scan(Page $page)
36
    {
37
        $this->currentPage = $page;
38
        $this->errors = [];
39
        $this->pageHtml = '';
40
41
        if (false !== $page->getRedirection()) {
1 ignored issue
show
Bug introduced by
The method getRedirection() does not exist on PiedWeb\CMSBundle\Entity\PageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to PiedWeb\CMSBundle\Entity\PageInterface. ( Ignorable by Annotation )

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

41
        if (false !== $page->/** @scrutinizer ignore-call */ getRedirection()) {
Loading history...
42
            // check $page->getRedirection() return 20X
43
44
            return true; // or status code
45
        }
46
47
        $template = null !== $page->getTemplate() ? $page->getTemplate() : $this->defaultTemplate;
1 ignored issue
show
Bug introduced by
The method getTemplate() does not exist on PiedWeb\CMSBundle\Entity\PageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to PiedWeb\CMSBundle\Entity\PageInterface. ( Ignorable by Annotation )

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

47
        $template = null !== $page->/** @scrutinizer ignore-call */ getTemplate() ? $page->getTemplate() : $this->defaultTemplate;
Loading history...
48
        $this->pageHtml = $this->twig->render($template, ['page' => $page]);
49
50
        // 2. Je récupère tout les liens et je les check
51
        // href="", data-rot="" data-img="", src="", data-bg
52
        $this->checkLinkedDocs($this->getLinkedDocs());
53
54
        return empty($this->errors) ? true : $this->errors;
55
    }
56
57
    protected function addError($message)
58
    {
59
        $this->errors[] = [
60
            'message' => $message,
61
            'page' => $this->currentPage,
62
        ];
63
    }
64
65
    protected function getLinkedDocs(): array
66
    {
67
        preg_match_all('/(href|data-rot|src|data-img|data-bg)=("\/[^"]*|\/[^\s>]*)/i', $this->pageHtml, $matches);
68
69
        $linkedDocs = [];
70
        foreach ($matches[0] as $k => $match) {
71
            $uri = ltrim('data-rot' == $matches[1][$k] ? str_rot13($matches[2][$k]) : $matches[2][$k], '"');
72
            $uri = strtok($uri, '#');
73
            if ('' !== $uri) {
74
                $linkedDocs[] = $uri;
75
            }
76
        }
77
78
        return array_unique($linkedDocs);
79
    }
80
81
    protected function checkLinkedDocs(array $linkedDocs)
82
    {
83
        foreach ($linkedDocs as $uri) {
84
            if (!$this->uriExist($uri)) {
85
                $this->addError('<code>'.$uri.'</code> introuvable');
86
            }
87
        }
88
    }
89
90
    protected function uriExist($uri)
91
    {
92
        $slug = ltrim($uri, '/');
93
94
        if (isset($this->everChecked[$slug])) {
95
            return $this->everChecked[$slug];
96
        }
97
98
        $checkDatabase = 0 !== strpos($slug, 'media/'); // we avoid to check in db the media, file exists is enough
99
        $page = true !== $checkDatabase ? null : $this->em->getRepository(get_class($this->currentPage))
100
            ->findOneBySlug('' == $slug ? 'homepage' : $slug);
1 ignored issue
show
Bug introduced by
The method findOneBySlug() does not exist on Doctrine\Persistence\ObjectRepository. Did you maybe mean findOneBy()? ( Ignorable by Annotation )

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

100
            ->/** @scrutinizer ignore-call */ findOneBySlug('' == $slug ? 'homepage' : $slug);

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...
101
102
        $this->everChecked[$slug] = (null === $page && !file_exists($this->webDir.'/'.$slug)) ? false : true;
103
104
        return $this->everChecked[$slug];
105
    }
106
}
107