SimpleConverter::convert()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ExternalFeedParser\Converters;
4
5
use ExternalFeedParser\Contracts\Converter;
6
use ExternalFeedParser\Entity\ExternalEntity;
7
use InvalidArgumentException;
8
9
class SimpleConverter implements Converter
10
{
11 4
    public function __construct(
12
        protected ?string $entityClass = null
13
    ) {
14 4
        if ($entityClass && !is_a($entityClass, ExternalEntity::class, true)) {
15 1
            throw new InvalidArgumentException("External Entity has wrong class [{$entityClass}].");
16
        }
17 3
    }
18
19 3
    public function convert($data): ?ExternalEntity
20
    {
21 3
        if ($this->entityClass) {
22 2
            return new $this->entityClass((array) $data);
23
        }
24
25 1
        return new ExternalEntity((array) $data);
26
    }
27
}
28