Completed
Push — master ( 13936d...d3fb26 )
by Andreas
03:07
created

FormatterService::getFormatter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 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 3
    protected static function getList()
48
    {
49 3
        if (! self::$list) {
50 1
            $finder = new FileFinder();
51 1
            $finder->addFilter(new ClassIsInstanceof(['Org_Heigl\DateFormatter\Formatter\FormatterInterface']));
52 1
            $finder->setFileList(new ClassMapList());
53 1
            foreach (self::$formatterDirectories as $directory) {
54 1
                $finder->addDirectory($directory);
55 1
            }
56 1
            self::$list = $finder->find();
57 1
        }
58
59 3
        return self::$list;
60
    }
61
62 3
    public static function getFormatter($format)
63
    {
64 3
        foreach (self::getList() as $class => $path) {
65 3
            if ($class::getFormatString() != $format) {
66 3
                continue;
67
            }
68
69 2
            return new $class();
70 1
        }
71
72 1
        throw new UnknownFormatException('There is no formater for this format');
73
    }
74
75
    /**
76
     * @param string $folder
77
     *
78
     * @return void
79
     */
80 1
    public static function addFormatterFolder($folder)
81
    {
82 1
        if (! is_dir($folder)) {
83 1
            return;
84
        }
85
86 1
        array_unshift(self::$formatterDirectories, $folder);
87 1
        self::$list = null;
88 1
    }
89
90
91
}