YamlLibraryRegister::getAllEntries()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Matks\MarkdownBlogBundle\Blog\Register;
4
5
use Matks\MarkdownBlogBundle\Blog\Post;
6
use Symfony\Component\Yaml\Yaml;
7
8
class YamlLibraryRegister implements LibraryRegisterInterface
9
{
10
    /**
11
     * @var RegisterEntry[]
12
     */
13
    private $entries = [];
14
15
    /**
16
     * @param string $registerFilepath
17
     */
18
    public function __construct($registerFilepath)
19
    {
20 1
        $this->validateRegisterFilepath($registerFilepath);
21
22 1
        $registerContent = Yaml::parse(file_get_contents(realpath($registerFilepath)));
23
24 1
        $this->parseRegisterContent($registerContent);
25 1
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function isRegistered($name)
31
    {
32 1
        return (true === isset($this->entries[$name]));
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getEntry($name)
39
    {
40 1
        if (false === $this->entries[$name]) {
41
            return;
42
        }
43
44 1
        return $this->entries[$name];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getAllEntries()
51
    {
52 1
        return $this->entries;
53
    }
54
55
    /**
56
     * @param string $registerFilepath
57
     *
58
     * @throws \InvalidArgumentException
59
     */
60
    private function validateRegisterFilepath($registerFilepath)
61
    {
62 1
        if (false === file_exists($registerFilepath)) {
63 1
            throw new \InvalidArgumentException("File $registerFilepath does not exist");
64
        }
65
66 1
        $yamlPattern = '#' . '(.*)\.yml$' . '#';
67
68 1
        $isAYamlFile = preg_match($yamlPattern, $registerFilepath);
69 1
        if (false === $isAYamlFile) {
70
            throw new \InvalidArgumentException("File $registerFilepath is not a YAML file");
71
        }
72 1
    }
73
74
    private function parseRegisterContent($registerContent)
75
    {
76 1
        $moreThanOneKey                 = (count($registerContent) !== 1);
77 1
        $uniqueKeyDifferentFromRegister = (false === array_key_exists('library_entries', $registerContent));
78
79 1
        if ($moreThanOneKey || $uniqueKeyDifferentFromRegister) {
80
            throw new \RuntimeException("Yaml library register file is not valid, expects a single node 'library_entries'");
81
        }
82
83 1
        $entries = $registerContent['library_entries'];
84
85 1
        foreach ($entries as $postName => $entry) {
86 1
            $publishDate = null;
87 1
            $category    = null;
88 1
            $tags        = [];
89 1
            $alias       = null;
90 1
            $url       = null;
91 1
            $blogType = Post::TYPE_STANDARD;
92
93 1
            if (isset($entry['date'])) {
94 1
                $publishDate = $entry['date'];
95 1
            }
96 1
            if (isset($entry['category'])) {
97 1
                $category = $entry['category'];
98 1
            }
99 1
            if (isset($entry['tags'])) {
100 1
                $tags = $entry['tags'];
101 1
            }
102 1
            if (isset($entry['alias'])) {
103 1
                $alias = $entry['alias'];
104 1
            }
105 1
            if (isset($entry['external'])) {
106
                $url = $entry['external'];
107
                $blogType = Post::TYPE_EXTERNAL;
108
            }
109
110 1
            $registerEntry            = new RegisterEntry($postName, $publishDate, $category, $tags, $alias, $blogType, $url);
111 1
            $this->entries[$postName] = $registerEntry;
112 1
        }
113 1
    }
114
}
115