SaasClientRelatedEntityTypeGuesser::guessType()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 19
rs 9.7998
1
<?php
2
3
/*
4
 * This file is part of the SaasProviderBundle package.
5
 * (c) Fluxter <http://fluxter.net/>
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 Fluxter\SaasProviderBundle\Form\TypeGuesser;
11
12
use Fluxter\SaasProviderBundle\Form\Type\ClientEntityType;
13
use Fluxter\SaasProviderBundle\Model\SaasClientRelatedInterface;
14
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
15
use Symfony\Component\Form\FormTypeGuesserInterface;
16
use Symfony\Component\Form\Guess\Guess;
17
use Symfony\Component\Form\Guess\TypeGuess;
18
19
class SaasClientRelatedEntityTypeGuesser extends DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
20
{
21
    public function guessType($class, $property)
22
    {
23
        if (!$ret = $this->getMetadata($class)) {
24
            return null;
25
        }
26
27
        list($metadata, $name) = $ret;
28
        if ($metadata->hasAssociation($property)) {
29
            $multiple = $metadata->isCollectionValuedAssociation($property);
30
            $mapping = $metadata->getAssociationMapping($property);
31
32
            $class = $mapping['targetEntity'];
33
            $interfaces = class_implements($class);
34
            if (in_array(SaasClientRelatedInterface::class, $interfaces)) {
35
                return new TypeGuess(ClientEntityType::class, [
36
                    'em' => $name,
37
                    'class' => $class,
38
                    'multiple' => $multiple,
39
                ], Guess::HIGH_CONFIDENCE);
40
            }
41
        }
42
    }
43
}
44