Passed
Push — master ( b8250a...45c3a8 )
by Dan
02:25
created

FontsFactory::addFont()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
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 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 is must contain a path to
27
     * a TTF/ODF font, or must be an array of those paths.
28
     *
29
     * @param string|array $fontsPath
0 ignored issues
show
Bug introduced by
There is no parameter named $fontsPath. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     * @param bool $isFontPath
31
     */
32 14
    protected function __construct($fonts, $isFontPath = true)
33
    {
34 14
        if ($isFontPath) {
35 12
            $this->loadFonts($fonts);
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