FileParser::assertYamlKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 10
Ratio 76.92 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 13
rs 9.4286
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/9/15
5
 * Time: 10:53 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\TodoFinder\Finder;
11
12
use Symfony\Component\Yaml\Yaml;
13
14
class FileParser
15
{
16
    const TODO_FINDER = 'todo_finder';
17
    const TOTAL_ALLOWED = 'total_allowed';
18
    const EXPRESSIONS = 'expressions';
19
20
    /**
21
     * @var array
22
     */
23
    private $file;
24
25
    /**
26
     * @param string $file
27
     */
28
    public function __construct($file)
29
    {
30
        $yaml = new Yaml();
31
        $file = (array) $yaml->parse($file, true);
32
33
        $this->validateFileStructure($file);
34
        $this->file = $file;
35
    }
36
37
    /**
38
     * @param array $file
39
     *
40
     * @throws FileParserException
41
     */
42
    private function validateFileStructure(array &$file)
43
    {
44 View Code Duplication
        if (false === array_key_exists(self::TODO_FINDER, $file)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            throw new FileParserException(
46
                sprintf(
47
                    'Provided YAML file does not compile with the required format. '
48
                    .'Expected \'%s\' key to be present but none was found.',
49
                    self::TODO_FINDER
50
                )
51
            );
52
        }
53
54
        $this->assertYamlKey($file[self::TODO_FINDER], self::TOTAL_ALLOWED);
55
        $this->assertYamlKey($file[self::TODO_FINDER], self::EXPRESSIONS);
56
    }
57
58
    /**
59
     * @param array  $file
60
     * @param string $key
61
     *
62
     * @throws FileParserException
63
     */
64
    private function assertYamlKey(array &$file, $key)
65
    {
66 View Code Duplication
        if (false === array_key_exists($key, $file)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            throw new FileParserException(
68
                sprintf(
69
                    'Provided YAML file does not compile with the required format. '
70
                    .'Expected \'%s\' key under \'%s\' to be present but none was found.',
71
                    $key,
72
                    self::TODO_FINDER
73
                )
74
            );
75
        }
76
    }
77
78
    /**
79
     * @throws FileParserException
80
     * @return int
81
     */
82
    public function getTotalAllowed()
83
    {
84
        $totalAllowed = $this->file[self::TODO_FINDER][self::TOTAL_ALLOWED];
85
        $totalAllowedInt = (int) $totalAllowed;
86
87
        if (!is_numeric($totalAllowed) && $totalAllowed != $totalAllowedInt) {
88
            throw new FileParserException(
89
                'Provided value for %s was expected to be an integer but %s was given',
90
                self::TOTAL_ALLOWED,
91
                gettype($totalAllowed)
92
            );
93
        }
94
95
        return (int) $totalAllowedInt;
96
    }
97
98
    /**
99
     * @throws FileParserException
100
     * @return array
101
     */
102
    public function getTodoExpressions()
103
    {
104
        $list = $this->file[self::TODO_FINDER][self::EXPRESSIONS];
105
106
        if (!is_array($list)) {
107
            throw new FileParserException(
108
                'Provided value for %s was expected to be an array but %s was given',
109
                self::EXPRESSIONS,
110
                gettype($list)
111
            );
112
        }
113
114
        arsort($list);
115
116
        return (array) $list;
117
    }
118
}
119