Collector::append()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
namespace Bookdown\Bookdown\Service;
3
4
use Psr\Log\LoggerInterface;
5
use Bookdown\Bookdown\Config\ConfigFactory;
6
use Bookdown\Bookdown\Content\IndexPage;
7
use Bookdown\Bookdown\Content\PageFactory;
8
use Bookdown\Bookdown\Content\Page;
9
use Bookdown\Bookdown\Fsio;
10
11
class Collector
12
{
13
    protected $pages = array();
14
    protected $configFactory;
15
    protected $pageFactory;
16
    protected $logger;
17
    protected $fsio;
18
    protected $level = 0;
19
    protected $prev;
20
21 6
    public function __construct(
22
        LoggerInterface $logger,
23
        Fsio $fsio,
24
        ConfigFactory $configFactory,
25
        PageFactory $pageFactory
26
    ) {
27 6
        $this->logger = $logger;
28 6
        $this->fsio = $fsio;
29 6
        $this->configFactory = $configFactory;
30 6
        $this->pageFactory = $pageFactory;
31 6
    }
32
33 1
    public function setRootConfigOverrides($rootConfigOverrides)
34
    {
35 1
        $this->configFactory->setRootConfigOverrides($rootConfigOverrides);
36 1
    }
37
38 3
    public function __invoke($configFile, $name = '', $parent = null, $count = 0)
39
    {
40 3
        $this->padlog("Collecting content from {$configFile}");
41 3
        $this->level ++;
42 3
        $index = $this->newIndex($configFile, $name, $parent, $count);
43 3
        $this->addContent($index);
0 ignored issues
show
Compatibility introduced by
$index of type object<Bookdown\Bookdown\Content\Page> is not a sub-type of object<Bookdown\Bookdown\Content\IndexPage>. It seems like you assume a child class of the class Bookdown\Bookdown\Content\Page to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
44 3
        $this->level --;
45 3
        return $index;
46
    }
47
48 3
    protected function newIndex($configFile, $name, $parent, $count)
49
    {
50 3
        if (! $parent) {
51 3
            return $this->addRootPage($configFile);
52
        }
53
54 3
        return $this->addIndexPage($configFile, $name, $parent, $count);
55
    }
56
57 3
    protected function addContent(IndexPage $index)
58
    {
59 3
        $count = 1;
60 3
        foreach ($index->getConfig()->getContent() as $name => $file) {
61 3
            $child = $this->newChild($file, $name, $index, $count);
62 3
            $index->addChild($child);
63 3
            $count ++;
64 3
        }
65 3
    }
66
67 3
    protected function newChild($file, $name, $index, $count)
68
    {
69 3
        $bookdown_json = 'bookdown.json';
70 3
        $len = -1 * strlen($bookdown_json);
71
72 3
        if (substr($file, $len) == $bookdown_json) {
73 3
            return $this->__invoke($file, $name, $index, $count);
74
        }
75
76 3
        return $this->addPage($file, $name, $index, $count);
77
    }
78
79 3
    protected function addPage($origin, $name, $parent, $count)
80
    {
81 3
        $page = $this->pageFactory->newPage($origin, $name, $parent, $count);
82 3
        $this->padlog("Added page {$page->getOrigin()}");
83 3
        return $this->append($page);
84
    }
85
86 3
    protected function addRootPage($configFile)
87
    {
88 3
        $data = $this->fsio->get($configFile);
89 3
        $config = $this->configFactory->newRootConfig($configFile, $data);
90 3
        $page = $this->pageFactory->newRootPage($config);
91 3
        $this->padlog("Added root page from {$configFile}");
92 3
        return $this->append($page);
93
    }
94
95 3
    protected function addIndexPage($configFile, $name, $parent, $count)
96
    {
97 3
        $data = $this->fsio->get($configFile);
98 3
        $config = $this->configFactory->newIndexConfig($configFile, $data);
99 3
        $page = $this->pageFactory->newIndexPage($config, $name, $parent, $count);
100 3
        $this->padlog("Added index page from {$configFile}");
101 3
        return $this->append($page);
102
    }
103
104 3
    protected function append(Page $page)
105
    {
106 3
        if ($this->prev) {
107 3
            $this->prev->setNext($page);
108 3
            $page->setPrev($this->prev);
109 3
        }
110 3
        $this->pages[] = $page;
111 3
        $this->prev = $page;
112 3
        return $page;
113
    }
114
115 3
    protected function padlog($str)
116
    {
117 3
        $pad = str_pad('', $this->level * 2);
118 3
        $this->logger->info("{$pad}{$str}");
119 3
    }
120
}
121