Completed
Pull Request — master (#6)
by Dan
07:19
created

FontsFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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