Metadata::constructFromFixtures()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hgraca\DoctrineTestDbRegenerationBundle\Doctrine;
6
7
use Doctrine\Common\DataFixtures\FixtureInterface;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
9
use Hgraca\DoctrineTestDbRegenerationBundle\StdLib\Filesystem;
10
use ReflectionClass;
11
12
final class Metadata
13
{
14
    /**
15
     * @var string
16
     */
17
    public $filename;
18
19
    /**
20
     * @var int
21
     */
22
    public $lastModifiedAt;
23
24 18
    private function __construct(string $filename, int $lastModifiedAt)
25
    {
26 18
        $this->filename = $filename;
27 18
        $this->lastModifiedAt = $lastModifiedAt;
28 18
    }
29
30
    /**
31
     * @return Metadata[]
32
     */
33 16
    public static function constructFromFixtures(array $fixtures): array
34
    {
35 16
        return static::sortList(
36 16
            array_map(
37 16
                function (FixtureInterface $fixture): Metadata {
38 16
                    return static::constructFromReflection(new ReflectionClass($fixture));
39 16
                },
40 16
                $fixtures
41
            )
42
        );
43
    }
44
45
    /**
46
     * @param ClassMetadata[] $entities
47
     *
48
     * @return Metadata[]
49
     */
50 2
    public static function constructFromEntities(array $entities): array
51
    {
52 2
        return static::sortList(
53 2
            array_map(
54 2
                function (ClassMetadata $entity): Metadata {
55 2
                    return static::constructFromReflection($entity->getReflectionClass());
56 2
                },
57 2
                $entities
58
            )
59
        );
60
    }
61
62 18
    private static function constructFromReflection(ReflectionClass $classInfo): self
63
    {
64 18
        $filename = $classInfo->getFileName();
65
66 18
        return new self($filename, Filesystem::filemtime($filename));
67
    }
68
69 18
    private static function sortList(array $list): array
70
    {
71 18
        usort(
72 18
            $list,
73 18
            function (Metadata $one, Metadata $other): int {
74 18
                return $one->compare($other);
75 18
            }
76
        );
77
78 18
        return $list;
79
    }
80
81 18
    private function compare(self $other): int
82
    {
83 18
        $dateDiff = $this->lastModifiedAt <=> $other->lastModifiedAt;
84
85 18
        if ($dateDiff === 0) {
86 18
            return strcmp($this->filename, $other->filename);
87
        }
88
89
        return $dateDiff;
90
    }
91
}
92