PlacerFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 80
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getInstance() 0 7 2
A getPlacersNames() 0 4 1
A getDefaultPlacer() 0 4 1
A getPlacer() 0 5 1
A getPlacerClass() 0 5 1
1
<?php
2
3
namespace SixtyNine\Cloud\Factory;
4
5
6
use SixtyNine\Cloud\Placer\CircularPlacer;
7
use SixtyNine\Cloud\Placer\LinearHorizontalPlacer;
8
use SixtyNine\Cloud\Placer\LinearVerticalPlacer;
9
use SixtyNine\Cloud\Placer\LissajouPlacer;
10
use SixtyNine\Cloud\Placer\PlacerInterface;
11
use SixtyNine\Cloud\Placer\SpiranglePlacer;
12
use SixtyNine\Cloud\Placer\WordlePlacer;
13
use Webmozart\Assert\Assert;
14
15
class PlacerFactory
16
{
17
    const PLACER_CIRCULAR = 'circular';
18
    const PLACER_WORDLE = 'wordle';
19
    const PLACER_SPIRANGLE = 'spirangle';
20
    const PLACER_LINEAR_H = 'linear-h';
21
    const PLACER_LINEAR_V = 'linear-v';
22
    const PLACER_LISSAJOU = 'lissajou';
23
24
    /** @var PlacerFactory */
25
    protected static $instance;
26
27
    /** @var array */
28
    protected $placers = array(
29
        self::PLACER_CIRCULAR => CircularPlacer::class,
30
        self::PLACER_WORDLE => WordlePlacer::class,
31
        self::PLACER_SPIRANGLE => SpiranglePlacer::class,
32
        self::PLACER_LINEAR_H => LinearHorizontalPlacer::class,
33
        self::PLACER_LINEAR_V => LinearVerticalPlacer::class,
34
        self::PLACER_LISSAJOU => LissajouPlacer::class,
35
    );
36
37
    /**
38
     * Disallow direct instantiation
39
     */
40
    protected function __construct() { }
41
42
    /**
43
     * @return PlacerFactory
44
     */
45 6
    public  static function getInstance()
46
    {
47 6
        if (!self::$instance) {
48 1
            self::$instance = new self();
49
        }
50 6
        return self::$instance;
51
    }
52
53
    /**
54
     * @return array
55
     */
56 1
    public function getPlacersNames()
57
    {
58 1
        return array_keys($this->placers);
59
    }
60
61
    /**
62
     * @param int $imgWidth
63
     * @param int $imgHeight
64
     * @return PlacerInterface
65
     */
66 2
    public function getDefaultPlacer($imgWidth, $imgHeight)
67
    {
68 2
        return $this->getPlacer(self::PLACER_CIRCULAR, $imgWidth, $imgHeight);
69
    }
70
71
    /**
72
     * @param string $name
73
     * @param int $imgWidth
74
     * @param int $imgHeight
75
     * @param int $increment
76
     * @return PlacerInterface
77
     */
78 6
    public function getPlacer($name, $imgWidth, $imgHeight, $increment = 10)
79
    {
80 6
        $className = $this->getPlacerClass($name);
81 6
        return new $className($imgWidth, $imgHeight, $increment);
82
    }
83
84
    /**
85
     * @param string $name
86
     * @return PlacerInterface
87
     * @throws \InvalidArgumentException
88
     */
89 6
    public function getPlacerClass($name)
90
    {
91 6
        Assert::keyExists($this->placers, $name, 'Placer not found: ' . $name);
92 6
        return $this->placers[$name];
93
    }
94
}