Passed
Push — master ( 4bccec...259a7c )
by David
01:44
created

PageRegistry::getImportedPagesFromCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\StaticRegistry\Registry;
5
6
7
use Psr\Container\ContainerInterface;
8
use Psr\SimpleCache\CacheInterface;
9
use Symfony\Component\Finder\Finder;
10
use TheCodingMachine\CMS\Block\BlockInterface;
11
use TheCodingMachine\CMS\StaticRegistry\Loaders\Page;
12
use TheCodingMachine\CMS\StaticRegistry\Loaders\Page404;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\CMS\Sta...egistry\Loaders\Page404 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * The page registry can fetch Page objects from the "pages" directory or from the container.
16
 */
17
class PageRegistry
18
{
19
    /**
20
     * @var string
21
     */
22
    private $pageDirectory;
23
    /**
24
     * @var CacheInterface
25
     */
26
    private $cache;
27
28
    /**
29
     * An array of pages indexed by URL.
30
     *
31
     * @var Page[][]
32
     */
33
    private $pages;
34
35
    public function __construct(string $pageDirectory, CacheInterface $cache)
36
    {
37
        $this->pageDirectory = rtrim($pageDirectory, '/\\').'/';
38
        $this->cache = $cache;
39
    }
40
41
    public function getPage(string $url, string $domain): Page
42
    {
43
        $pages = $this->getImportedPagesFromCache();
44
45
        if (isset($pages[$domain][$url])) {
46
            return $pages[$domain][$url];
47
        }
48
        if (isset($pages['<any>'][$url])) {
49
            return $pages['<any>'][$url];
50
        }
51
52
        throw PageNotFoundException::couldNotFindPage($url, $domain);
53
    }
54
55
    /**
56
     * @return Page[][]
57
     */
58
    private function getImportedPagesFromCache(): array
59
    {
60
        $key = 'pages';
61
        $pages = $this->cache->get($key);
62
        if ($pages === null) {
63
            $pages = $this->getImportedPages();
64
            $this->cache->set($key, $pages);
65
        }
66
        return $pages;
67
    }
68
69
    /**
70
     * @return Page[][]
71
     * @throws DuplicatePageException
72
     */
73
    private function getImportedPages(): array
74
    {
75
        if ($this->pages === null) {
76
            $fileList = new Finder();
77
78
            $fileList->files()->in($this->pageDirectory)->sortByName();
79
80
            foreach ($fileList as $file) {
81
                $importedPage = Page::fromFile($file->getRealPath());
82
83
                if (isset($this->pages[$importedPage->getWebsite() ?? '<any>'][$importedPage->getUrl()])) {
84
                    throw new DuplicatePageException(sprintf('There are 2 pages claiming the URL %s %s', $importedPage->getUrl(), $importedPage->getWebsite() ? ' of website '.$importedPage->getWebsite() : ''));
85
                }
86
                $this->pages[$importedPage->getWebsite() ?? '<any>'][$importedPage->getUrl()] = $importedPage;
87
            }
88
        }
89
        return $this->pages;
90
    }
91
}
92