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

Property   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFakerMethodFromDoctrineType() 0 9 2
A getPropertiesFromDoctrineRelations() 0 2 1
A __construct() 0 3 1
A getFakerMethodFromDoctrineFieldMappings() 0 19 4
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