Passed
Push — master ( ccac05...2a0bbc )
by Kevin
02:22
created

Property::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Zenstruck\Foundry\Bundle\Extractor;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Exception;
7
use ReflectionClass;
8
9
class Property
10
{
11
    /**
12
     * @var EntityManagerInterface
13
     */
14
    private $em;
15
16
    public function __construct(EntityManagerInterface $em)
17
    {
18
        $this->em = $em;
19
    }
20
21
    /**
22
     * @TODO Support for relations is missing
23
     */
24
    public function getFakerMethodFromDoctrineFieldMappings(ReflectionClass $entity): array
25
    {
26
        $properties = [];
27
        $classMetaData = $this->em->getClassMetadata($entity->getName());
28
        $identifierFieldNames = $classMetaData->getIdentifierFieldNames();
29
30
        foreach ($classMetaData->fieldMappings as $property) {
31
            // IGNORE FIELD IF IDENTIFIER
32
            if (\in_array($property['fieldName'], $identifierFieldNames)) {
33
                continue;
34
            }
35
36
            // CREATE FROM DOCTRINE TYPE IF PROP IS NOT NULLABLE
37
            if (!$property['nullable']) {
38
                $properties[$property['fieldName']] = $this->createFakerMethodFromDoctrineType($property['type']);
39
            }
40
        }
41
42
        return $properties;
43
    }
44
45
    /**
46
     * @throws Exception
47
     */
48
    public function createFakerMethodFromDoctrineType(string $doctrineType): string
49
    {
50
        $doctrineType = \mb_strtoupper($doctrineType);
51
52
        if (\array_key_exists($doctrineType, DoctrineTypes::DOCTRINE_TYPES)) {
53
            return DoctrineTypes::DOCTRINE_TYPES[$doctrineType];
54
        }
55
56
        return 'null, // @TODO add '.$doctrineType.' manually';
57
    }
58
59
    /**
60
     * @TODO get relations which cant be NULL
61
     */
62
    public function getPropertiesFromDoctrineRelations()
63
    {
64
    }
65
}
66