PropertyDefinition::getEventClassName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Core\Property\Factory;
19
20
use CuyZ\Notiz\Core\Exception\DuplicateEntryException;
21
use CuyZ\Notiz\Core\Exception\EntryNotFoundException;
22
use CuyZ\Notiz\Core\Property\PropertyEntry;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * This class is a definition for a given property name of a given event type,
27
 * for instance it can define the property "markers" of the event "a user has
28
 * registered".
29
 *
30
 * It contains any number of independent entries, for instance "user_name" or
31
 * "user_email".
32
 *
33
 * ---
34
 *
35
 * A property definition is built by an event, because the event is the only one
36
 * to know which entries can be added.
37
 *
38
 * @see \CuyZ\Notiz\Core\Event\Support\HasProperties::buildPropertyDefinition
39
 *
40
 * In the method above, the definition can be manipulated to add new entries:
41
 *
42
 * @see \CuyZ\Notiz\Core\Property\Factory\PropertyDefinition::addEntry
43
 *
44
 * ---
45
 *
46
 * Please note that an event may handle several properties, for instance with
47
 * our last example the properties `markers` and `email` may be used:
48
 *
49
 * - `markers` contains concrete information that can be used in the message of
50
 *   the notification.
51
 * - `email` contains information about who will receive the notification.
52
 *
53
 * In this case, the two properties above have their own definition.
54
 */
55
class PropertyDefinition
56
{
57
    /**
58
     * @var string
59
     */
60
    protected $eventClassName;
61
62
    /**
63
     * @var string
64
     */
65
    protected $propertyType;
66
67
    /**
68
     * @var PropertyEntry[]
69
     */
70
    protected $properties = [];
71
72
    /**
73
     * @param string $eventClassName
74
     * @param string $propertyType
75
     */
76
    public function __construct(string $eventClassName, string $propertyType)
77
    {
78
        $this->eventClassName = $eventClassName;
79
        $this->propertyType = $propertyType;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getEventClassName(): string
86
    {
87
        return $this->eventClassName;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getPropertyType(): string
94
    {
95
        return $this->propertyType;
96
    }
97
98
    /**
99
     * @param string $name
100
     * @return PropertyEntry
101
     *
102
     * @throws DuplicateEntryException
103
     */
104
    public function addEntry(string $name): PropertyEntry
105
    {
106
        if ($this->hasEntry($name)) {
107
            throw DuplicateEntryException::propertyEntryDuplication($name, $this->eventClassName, $this->propertyType);
108
        }
109
110
        $this->properties[$name] = GeneralUtility::makeInstance($this->propertyType, $name);
111
112
        return $this->properties[$name];
113
    }
114
115
    /**
116
     * @param string $name
117
     * @return bool
118
     */
119
    public function hasEntry(string $name): bool
120
    {
121
        return isset($this->properties[$name]);
122
    }
123
124
    /**
125
     * @param string $name
126
     * @return PropertyEntry
127
     *
128
     * @throws EntryNotFoundException
129
     */
130
    public function getEntry(string $name): PropertyEntry
131
    {
132
        if (false === $this->hasEntry($name)) {
133
            throw EntryNotFoundException::propertyEntryNotFound($name, $this->eventClassName, $this->propertyType, $this);
134
        }
135
136
        return $this->properties[$name];
137
    }
138
139
    /**
140
     * @return PropertyEntry[]
141
     */
142
    public function getEntries(): array
143
    {
144
        return $this->properties;
145
    }
146
}
147