FontsFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 93
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A create() 0 4 1
A loadFonts() 0 7 2
A addFont() 0 6 1
A getFont() 0 5 1
A getImagineFont() 0 5 1
A getFonts() 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
     * Initialize the FontsFactory.
21
     *
22
     * If $isFontPath is true then the first parameter is considered as a path to
23
     * a directory containing the fonts. This directory is scanned for TTF and OTF
24
     * fonts.
25
     *
26
     * If $isFontPath is false, then the first parameter must contain a path to
27
     * a TTF/ODF font, or must be an array of those paths.
28
     *
29
     * @param string|array $fonts
30
     * @param bool $isFontPath
31
     */
32 14
    protected function __construct($fonts, $isFontPath = true)
33
    {
34 14
        if ($isFontPath) {
35 12
            $this->loadFonts($fonts);
0 ignored issues
show
Bug introduced by
It seems like $fonts defined by parameter $fonts on line 32 can also be of type array; however, SixtyNine\Cloud\Factory\FontsFactory::loadFonts() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
36 12
            return;
37
        }
38
39 2
        $fonts = is_array($fonts) ? $fonts : [$fonts];
40 2
        foreach ($fonts as $font) {
41 2
            $this->addFont($font);
42
        }
43 2
    }
44
45
    /**
46
     * @param string $fonts
47
     * @param bool $isFontPath
48
     * @return FontsFactory
49
     */
50 14
    public static function create($fonts, $isFontPath = true)
51
    {
52 14
        return new self($fonts, $isFontPath);
53
    }
54
55
    /**
56
     * @param string $fontsPath
57
     */
58 12
    public function loadFonts($fontsPath)
59
    {
60 12
        Assert::fileExists($fontsPath, 'The fonts path must exist');
61 12
        foreach (glob($fontsPath . '/*.{ttf,otf}', GLOB_BRACE) as $filename) {
62 12
            $this->addFont($filename);
63
        }
64 12
    }
65
66
    /**
67
     * @param string $filename
68
     */
69 14
    protected function addFont($filename)
70
    {
71 14
        Assert::fileExists($filename, 'The font file must exist');
72 14
        $name = basename($filename);
73 14
        $this->fonts[$name] = realpath($filename);
74 14
    }
75
76
    /**
77
     * @param string $name
78
     * @return Font
79
     * @throws \InvalidArgumentException
80
     */
81 13
    public function getFont($name)
82
    {
83 13
        Assert::keyExists($this->fonts, $name, 'Font not found: ' . $name);
84 13
        return new Font($name, $this->fonts[$name]);
85
    }
86
87
    /**
88
     * @param string $name
89
     * @param int $size
90
     * @param string $color
91
     * @return ImagineFont
92
     */
93 13
    public function getImagineFont($name, $size, $color = '#000000')
94
    {
95 13
        $font = $this->getFont($name);
96 13
        return new ImagineFont($font->getFile(), $size, new Color($color));
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getFonts()
103
    {
104
        return array_keys($this->fonts);
105
    }
106
}
107