HydratorGenerator::getHydratorDir()   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
3
/*
4
 * (c) Olivier Laviale <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace olvlvl\DoctrineGenerators\Document;
11
12
use Doctrine\ODM\MongoDB\DocumentManager;
13
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
14
15
class HydratorGenerator
16
{
17
    use EnsureDirectory;
18
19
    /**
20
     * @var DocumentManager
21
     */
22
    private $documentManager;
23
24
    public function __construct(DocumentManager $documentManager)
25
    {
26
        $this->documentManager = $documentManager;
27
    }
28
29
    /**
30
     * @return string[]|null
31
     */
32
    public function __invoke(): array
33
    {
34
        $metadataCollection = $this->getMetadataCollection();
35
36
        if (count($metadataCollection) === 0) {
37
            return [];
38
        }
39
40
        $this->getHydratorFactory()->generateHydratorClasses(
41
            $metadataCollection,
42
            $this->ensureDirectory($this->getHydratorDir())
43
        );
44
45
        return array_map(function ($metadata) {
46
            return $metadata->name;
47
        }, $metadataCollection);
48
    }
49
50
    private function getMetadataCollection(): array
51
    {
52
        return $this->documentManager->getMetadataFactory()->getAllMetadata();
53
    }
54
55
    private function getHydratorDir(): string
56
    {
57
        return $this->documentManager->getConfiguration()->getHydratorDir();
58
    }
59
60
    private function getHydratorFactory(): HydratorFactory
61
    {
62
        return $this->documentManager->getHydratorFactory();
63
    }
64
}
65