GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GlossaryLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * This file is part of the trefoil application.
4
 *
5
 * (c) Miguel Angel Gabriel <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Trefoil\Helpers;
12
13
use EasySlugger\SluggerInterface;
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * Loader class for Glossary object from definition file.
18
 * Expected format (yml):
19
 *     glossary:
20
 *         options: # optional section
21
 *             <option..>: <value>
22
 *         terms:
23
 *             term1: description of term 1
24
 *             ...
25
 *             termn: description of term n
26
 *
27
 * @see GlossaryReplacer for allowed options
28
 * @see Glossary for term syntax
29
 */
30
class GlossaryLoader
31
{
32
33
    protected $fileName;
34
    protected $terms;
35
    protected $options;
36
    protected $loaded = false;
37
    protected $slugger;
38
39
    /**
40
     * @param string           $fileName (full path) of the definition file
41
     * @param SluggerInterface $slugger  An Slugger instance
42
     */
43 1
    public function __construct($fileName, SluggerInterface $slugger)
44
    {
45 1
        $this->fileName = $fileName;
46 1
        $this->slugger = $slugger;
47 1
    }
48
49
    /**
50
     * Load the definition file.
51
     *
52
     * @param bool $hasOptions Whether the file also has an 'options' section
53
     *
54
     * @return Glossary
55
     */
56 3
    public function load($hasOptions = false)
57
    {
58 3
        $this->readFromFile($hasOptions);
59 3
        $glossary = $this->parse();
60
61 3
        return $glossary;
62
    }
63
64
    /**
65
     * True if the definition file could not been loaded (i.e. not found)
66
     *
67
     * @return boolean
68
     */
69 3
    public function isLoaded()
70
    {
71 3
        return $this->loaded;
72
    }
73
74
    /**
75
     * @return array
76
     */
77 3
    public function getOptions()
78
    {
79 3
        return $this->options;
80
    }
81
82
    /**
83
     * Read the options file
84
     *
85
     * @param bool $hasOptions Whether the file also has an 'options' section
86
     */
87 1
    protected function readFromFile($hasOptions = false)
88
    {
89 1
        $this->loaded = false;
90
91 1
        if (file_exists($this->fileName)) {
92 1
            $glossaryDefinition = Yaml::parse($this->fileName);
93 1
            $this->loaded = true;
94 1
        } else {
95 1
            $glossaryDefinition = array();
96
        }
97
98
        // defaults
99
        $default = array(
100
            'glossary' => array(
101 1
                'terms' => array()
102 1
            )
103 1
        );
104
105 1
        if ($hasOptions) {
106 1
            $default['glossary']['options'] = array(
107 1
                'coverage' => 'item',
108
                'elements' => array(
109
                    'chapter'
110 1
                )
111 1
            );
112 1
        }
113
114 1
        $glossaryDefinition = array_replace_recursive($default, $glossaryDefinition);
115
116 1
        $this->terms = $glossaryDefinition['glossary']['terms'];
117 1
        if ($hasOptions) {
118 1
            $this->options = $glossaryDefinition['glossary']['options'];
119 1
        }
120 1
    }
121
122
    /**
123
     * Parse the loaded definitions into a Glossary object
124
     *
125
     * @return \Trefoil\Helpers\Glossary
126
     */
127 1
    protected function parse()
128
    {
129 1
        $glossary = new Glossary();
130
131 1
        foreach ($this->terms as $term => $description) {
132
133 1
            $gi = new GlossaryItem();
134 1
            $gi->setTerm($term);
135
136
            // ensure uniqueness of slug to avoid collisions
137
            // with other files that define the same term
138
            // with different variant
139 1
            $prefix = crc32($term . $description) . '-';
140
141 1
            $gi->setSlug($prefix . $this->slugger->slugify($term));
142 1
            $gi->setSource(basename($this->fileName));
143 1
            $gi->setDescription($description);
144
145 1
            $glossary->add($gi);
146 1
        }
147
148 1
        return $glossary;
149
    }
150
151
}
152