Passed
Push — master ( 6e26c0...38e037 )
by Dan
02:58
created

FontsFactory::loadFonts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
4
namespace SixtyNine\Cloud\Factory;
5
6
use Imagine\Gd\Imagine;
7
use Imagine\Image\Box;
8
use Imagine\Image\Color;
9
use Imagine\Image\Point;
10
use Imagine\Gd\Font as ImagineFont;
11
use SixtyNine\Cloud\Model\Font;
12
13
class FontsFactory
14
{
15
    /** @var string */
16
    protected $fontsPath;
17
    /** @var array */
18
    protected $fonts = array();
19
20 7
    protected function __construct($fontsPath)
21
    {
22 7
        $this->fontsPath = $fontsPath;
23 7
        $this->loadFonts();
24 7
    }
25
26 7
    public  static function create($fontsPath)
27
    {
28 7
        if (!file_exists($fontsPath)) {
29
            throw new \InvalidArgumentException('The fonts path must exist');
30
        }
31
32 7
        return new self($fontsPath);
33
    }
34
35 7
    public function loadFonts()
36
    {
37 7
        foreach (glob($this->fontsPath . "/*.ttf") as $filename) {
38 7
            $name = basename($filename);
39 7
            $this->fonts[$name] = new Font($name, realpath($filename));
40
        }
41 7
    }
42
43
    /**
44
     * @param string $name
45
     * @return Font
46
     * @throws \InvalidArgumentException
47
     */
48 6
    public function getFont($name)
49
    {
50 6
        if (!array_key_exists($name, $this->fonts)) {
51
            throw new \InvalidArgumentException('Font not found: ' . $name);
52
        }
53
54 6
        return $this->fonts[$name];
55
    }
56
57
    /**
58
     * @param string $name
59
     * @param int $size
60
     * @param string $color
61
     * @return ImagineFont
62
     */
63 6
    public function getImagineFont($name, $size, $color = '#000000')
64
    {
65 6
        $font = $this->getFont($name);
66 6
        return new ImagineFont($font->getFile(), $size, new Color($color));
67
    }
68
69
    public function getFonts()
70
    {
71
        return array_keys($this->fonts);
72
    }
73
}
74