Passed
Pull Request — master (#13)
by Aidas
02:38
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 Method

Rating   Name   Duplication   Size   Complexity  
A FontsFactory::create() 0 4 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 array */
17
    protected $fonts = array();
18
19
    /**
20
     * @param string|array $fontsPath
21
     */
22 12
    protected function __construct($fontsPath)
23
    {
24 12
        if (is_array($fontsPath)) {
25
            foreach ($fontsPath as $filename) {
26
                Assert::fileExists($filename, 'The fonts path must exist');
27
                $name = basename($filename);
28
                $this->fonts[$name] = new Font($name, realpath($filename));
29
            }
30
        } else {
31 12
            Assert::fileExists($fontsPath, 'The fonts path must exist');
32 12
            foreach (glob($fontsPath . "/*.ttf") as $filename) {
33 12
                $name = basename($filename);
34 12
                $this->fonts[$name] = new Font($name, realpath($filename));
35
            }
36
        }
37 12
    }
38
39
    /**
40
     * @param string $fontsPath
41
     * @return FontsFactory
42
     */
43 12
    public static function create($fontsPath)
44
    {
45 12
        return new self($fontsPath);
46
    }
47
48
    /**
49
     * @param string $name
50
     * @return Font
51
     * @throws \InvalidArgumentException
52
     */
53 11
    public function getFont($name)
54
    {
55 11
        Assert::keyExists($this->fonts, $name, 'Font not found: ' . $name);
56 11
        return $this->fonts[$name];
57
    }
58
59
    /**
60
     * @param string $name
61
     * @param int $size
62
     * @param string $color
63
     * @return ImagineFont
64
     */
65 11
    public function getImagineFont($name, $size, $color = '#000000')
66
    {
67 11
        $font = $this->getFont($name);
68 11
        return new ImagineFont($font->getFile(), $size, new Color($color));
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getFonts()
75
    {
76
        return array_keys($this->fonts);
77
    }
78
}
79