FormatterService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 8
c 4
b 1
f 1
lcom 1
cbo 4
dl 0
loc 61
ccs 24
cts 24
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addFormatterFolder() 0 9 2
A getList() 0 14 3
A getFormatter() 0 12 3
1
<?php
2
3
/**
4
 * Copyright (c) 2016-2016} Andreas Heigl<[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 *
24
 * @author    Andreas Heigl<[email protected]>
25
 * @copyright 2016-2016 Andreas Heigl
26
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
27
 * @version   0.0
28
 * @since     24.03.2016
29
 * @link      http://github.com/heiglandreas/org.heigl.DateFormatter
30
 */
31
32
namespace Org_Heigl\DateFormatter\Service;
33
34
use Org_Heigl\DateFormatter\UnknownFormatException;
35
use Org_Heigl\FileFinder\ClassMapList;
36
use Org_Heigl\FileFinder\FileFinder;
37
use Org_Heigl\FileFinder\Filter\ClassIsInstanceof;
38
39
class FormatterService
40
{
41
    protected static $list;
42
43
    protected static $formatterDirectories = [
44
        __DIR__ . '/../Formatter/',
45
    ];
46
47
    /**
48
     * @return \Org_Heigl\FileFinder\FileListInterface
49
     */
50 5
    protected static function getList()
51
    {
52 5
        if (! self::$list) {
53 1
            $finder = new FileFinder();
54 1
            $finder->addFilter(new ClassIsInstanceof(['Org_Heigl\DateFormatter\Formatter\FormatterInterface']));
55 1
            $finder->setFileList(new ClassMapList());
56 1
            foreach (self::$formatterDirectories as $directory) {
57 1
                $finder->addDirectory($directory);
58 1
            }
59 1
            self::$list = $finder->find();
60 1
        }
61
62 5
        return self::$list;
63
    }
64
65
    /**
66
     * @param string $format
67
     *
68
     * @return Org_Heigl\DateFormatter\Formatter\FormatterInterface
69
     */
70 5
    public static function getFormatter($format)
71
    {
72 5
        foreach (self::getList() as $class => $path) {
0 ignored issues
show
Bug introduced by
The expression self::getList() of type object<Org_Heigl\FileFinder\FileListInterface> is not traversable.
Loading history...
73 5
            if (! in_array(strtolower($format), array_map('strtolower', $class::getFormatString()))) {
74 4
                continue;
75
            }
76
77 3
            return new $class();
78 2
        }
79
80 2
        throw new UnknownFormatException('There is no formater for this format');
81
    }
82
83
    /**
84
     * @param string $folder
85
     *
86
     * @return void
87
     */
88 1
    public static function addFormatterFolder($folder)
89
    {
90 1
        if (! is_dir($folder)) {
91 1
            return;
92
        }
93
94 1
        array_unshift(self::$formatterDirectories, $folder);
95 1
        self::$list = null;
96 1
    }
97
98
99
}
100