Lines
96.96%
64 / 66
Methods
60.00%
3 / 5
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| create | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getEntity | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| entityClass | 98.00% | 49 / 50 | 0.00% | 0 / 1 | 2 | ||
| getEntityName | 90.90% | 10 / 11 | 0.00% | 0 / 1 | 3.01 | ||
| 10 | class Factory | |
| 11 | { | |
| 12 | /** | |
| 13 | * @var mixed $data unserialized entity | |
| 14 | */ | |
| 15 | protected $data = null; | |
| 16 | ||
| 17 | public function __construct(mixed $data) | |
| 18 | { | |
| 19 | $this->data = $data; | |
| 20 | } | |
| 21 | ||
| 22 | public static function create(mixed $data): self | |
| 23 | { | |
| 24 | return new self($data); | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * Detect type of entity and return initialized object | |
| 29 | * | |
| 30 | * @return Entity | |
| 31 | */ | |
| 32 | public function getEntity() | |
| 33 | { | |
| 34 | $class = self::entityClass($this->getEntityName()); | |
| 35 | /** @var Entity $entity */ | |
| 36 | $entity = new $class(new UnflattedArray($this->data)); | |
| 37 | return $entity; | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Schema names are the lowercased class, with the first letter restored. | |
| 42 | * A few older names only capitalize that first letter. | |
| 43 | * | |
| 44 | * @return class-string<Entity> | |
| 45 | */ | |
| 46 | private static function entityClass(string $entityName): string | |
| 47 | { | |
| 48 | $classes = [ | |
| 49 | 'Apiclient' => \BO\Zmsentities\Apiclient::class, | |
| 50 | 'Apikey' => \BO\Zmsentities\Apikey::class, | |
| 51 | 'Appointment' => \BO\Zmsentities\Appointment::class, | |
| 52 | 'Availability' => \BO\Zmsentities\Availability::class, | |
| 53 | 'AvailabilityHistory' => \BO\Zmsentities\AvailabilityHistory::class, | |
| 54 | 'Calendar' => \BO\Zmsentities\Calendar::class, | |
| 55 | 'Calldisplay' => \BO\Zmsentities\Calldisplay::class, | |
| 56 | 'Client' => \BO\Zmsentities\Client::class, | |
| 57 | 'Closure' => \BO\Zmsentities\Closure::class, | |
| 58 | 'Cluster' => \BO\Zmsentities\Cluster::class, | |
| 59 | 'Config' => \BO\Zmsentities\Config::class, | |
| 60 | 'Contact' => \BO\Zmsentities\Contact::class, | |
| 61 | 'Day' => \BO\Zmsentities\Day::class, | |
| 62 | 'Dayoff' => \BO\Zmsentities\Dayoff::class, | |
| 63 | 'Department' => \BO\Zmsentities\Department::class, | |
| 64 | 'Eventlog' => \BO\Zmsentities\EventLog::class, | |
| 65 | 'Exchange' => \BO\Zmsentities\Exchange::class, | |
| 66 | 'Ics' => \BO\Zmsentities\Ics::class, | |
| 67 | 'Link' => \BO\Zmsentities\Link::class, | |
| 68 | 'Log' => \BO\Zmsentities\Log::class, | |
| 69 | 'Mail' => \BO\Zmsentities\Mail::class, | |
| 70 | 'Mailtemplate' => \BO\Zmsentities\Mailtemplate::class, | |
| 71 | 'Metaresult' => \BO\Zmsentities\Metaresult::class, | |
| 72 | 'Mimepart' => \BO\Zmsentities\Mimepart::class, | |
| 73 | 'Month' => \BO\Zmsentities\Month::class, | |
| 74 | 'Organisation' => \BO\Zmsentities\Organisation::class, | |
| 75 | 'Owner' => \BO\Zmsentities\Owner::class, | |
| 76 | 'Permission' => \BO\Zmsentities\Permission::class, | |
| 77 | 'Process' => \BO\Zmsentities\Process::class, | |
| 78 | 'Processarchived' => \BO\Zmsentities\Processarchived::class, | |
| 79 | 'Provider' => \BO\Zmsentities\Provider::class, | |
| 80 | 'Queue' => \BO\Zmsentities\Queue::class, | |
| 81 | 'Request' => \BO\Zmsentities\Request::class, | |
| 82 | 'Requestrelation' => \BO\Zmsentities\RequestRelation::class, | |
| 83 | 'Requeststatistic' => \BO\Zmsentities\Requeststatistic::class, | |
| 84 | 'Role' => \BO\Zmsentities\Role::class, | |
| 85 | 'Scope' => \BO\Zmsentities\Scope::class, | |
| 86 | 'Session' => \BO\Zmsentities\Session::class, | |
| 87 | 'Slot' => \BO\Zmsentities\Slot::class, | |
| 88 | 'Source' => \BO\Zmsentities\Source::class, | |
| 89 | 'Status' => \BO\Zmsentities\Status::class, | |
| 90 | 'Ticketprinter' => \BO\Zmsentities\Ticketprinter::class, | |
| 91 | 'Useraccount' => \BO\Zmsentities\Useraccount::class, | |
| 92 | 'Useraccountprofile' => \BO\Zmsentities\Useraccountprofile::class, | |
| 93 | 'Workstation' => \BO\Zmsentities\Workstation::class, | |
| 94 | ]; | |
| 95 | ||
| 96 | if (!isset($classes[$entityName])) { | |
| 97 | throw new \InvalidArgumentException('Unknown schema entity'); | |
| 98 | } | |
| 99 | ||
| 100 | return $classes[$entityName]; | |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Parse schema and return Entity name | |
| 105 | * | |
| 106 | * @return string | |
| 107 | */ | |
| 108 | public function getEntityName() | |
| 109 | { | |
| 110 | if (!Property::__keyExists('$schema', $this->data)) { | |
| 111 | throw new \BO\Zmsentities\Exception\SchemaMissingKey('Missing $schema-key on given data.'); | |
| 112 | } | |
| 113 | $schema = $this->data['$schema']; | |
| 114 | $entityName = is_string($schema) | |
| 115 | ? (preg_replace('#^.*/([^/]+)\.json#', '$1', $schema) ?? '') | |
| 116 | : ''; | |
| 117 | $entityName = ucfirst($entityName); | |
| 118 | ||
| 119 | // jsonSerialize() lowercases the class short name (AvailabilityHistory → | |
| 120 | // availabilityhistory.json). ucfirst then yields Availabilityhistory, which | |
| 121 | // PSR-4 cannot autoload on Linux. | |
| 122 | $classAliases = [ | |
| 123 | 'Availabilityhistory' => 'AvailabilityHistory', | |
| 124 | ]; | |
| 125 | ||
| 126 | return $classAliases[$entityName] ?? $entityName; | |
| 127 | } | |
| 128 | } |