Factory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 68
ccs 12
cts 12
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createEvent() 0 10 2
A isEventIDValid() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Event;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Event;
17
use RuntimeException;
18
use SebastianFeldmann\Git\Repository;
19
20
/**
21
 * Event Factory
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhook-git/captainhook
26
 * @since   Class available since Release 5.11.0
27
 */
28
class Factory
29
{
30
    /**
31
     * @var \CaptainHook\App\Config
32
     */
33
    protected $config;
34
35
    /**
36
     * @var \CaptainHook\App\Console\IO
37
     */
38
    private $io;
39
40
    /**
41
     * @var \SebastianFeldmann\Git\Repository
42
     */
43
    private $repository;
44
45
    /**
46
     * List of available events
47
     *
48
     * @var string[]
49
     */
50
    private $validEventIDs = [
51
        'onHookFailure' => HookFailed::class,
52
        'onHookSuccess' => HookSucceeded::class,
53
    ];
54
55
    /**
56
     * Event Factory
57
     *
58
     * @param \CaptainHook\App\Console\IO       $io
59
     * @param \CaptainHook\App\Config           $config
60
     * @param \SebastianFeldmann\Git\Repository $repository
61
     */
62 59
    public function __construct(IO $io, Config $config, Repository $repository)
63
    {
64 59
        $this->config     = $config;
65 59
        $this->io         = $io;
66 59
        $this->repository = $repository;
67
    }
68
69
    /**
70
     * Create a CaptainHook event
71
     *
72
     * @param string $name
73
     * @return \CaptainHook\App\Event
74
     */
75 23
    public function createEvent(string $name): Event
76
    {
77 23
        if (!$this->isEventIDValid($name)) {
78 1
            throw new RuntimeException('invalid event name: ' . $name);
79
        }
80
81 22
        $class = $this->validEventIDs[$name];
82
        /** @var \CaptainHook\App\Event $event */
83 22
        $event = new $class($this->io, $this->config, $this->repository);
84 22
        return $event;
85
    }
86
87
    /**
88
     * Validates an event name
89
     *
90
     * @param  string $name
91
     * @return bool
92
     */
93 23
    public function isEventIDValid(string $name): bool
94
    {
95 23
        return array_key_exists($name, $this->validEventIDs);
96
    }
97
}
98