ParameterConverter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 52
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertFromEntity() 0 15 2
A convert() 0 13 2
1
<?php
2
3
namespace Victoire\Bundle\BusinessEntityBundle\Converter;
4
5
use Symfony\Component\PropertyAccess\PropertyAccessor;
6
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty;
7
8
/**
9
 * Parameter Converter
10
 * ref: victoire_business_entity.converter.parameter_converter.
11
 */
12
class ParameterConverter
13
{
14
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$url" missing
Loading history...
15
     * Replace the code string with the value of the entity attribute.
16
     *
17
     * @param BusinessProperty $businessProperty
0 ignored issues
show
introduced by
Doc comment for parameter $businessProperty does not match actual variable name $url
Loading history...
18
     * @param object           $entity
0 ignored issues
show
introduced by
Doc comment for parameter $entity does not match actual variable name $businessProperty
Loading history...
19
     *
20
     * @throws \Exception
21
     *
22
     * @return string The updated string
23
     */
24
    public function convertFromEntity($url, BusinessProperty $businessProperty, $entity)
25
    {
26
        //test parameters
27
        if ($entity === null) {
28
            throw new \Exception('The parameter entity can not be null');
29
        }
30
        //the attribute to set
31
        $entityProperty = $businessProperty->getName();
32
33
        //the value of the attribute
34
        $accessor = new PropertyAccessor();
35
        $attributeValue = $accessor->getValue($entity, $entityProperty);
36
37
        return $this->convert($url, 'item.'.$entityProperty, $attributeValue);
38
    }
39
40
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$url" missing
Loading history...
41
     * Replace the code string with the value of the entity attribute.
42
     *
43
     * @param string $entityProperty
0 ignored issues
show
introduced by
Doc comment for parameter $entityProperty does not match actual variable name $url
Loading history...
44
     * @param string $attributeValue
0 ignored issues
show
introduced by
Doc comment for parameter $attributeValue does not match actual variable name $entityProperty
Loading history...
45
     *
46
     * @throws \Exception
47
     *
48
     * @return string The updated string
49
     */
50
    public function convert($url, $entityProperty, $attributeValue)
51
    {
52
        //the string to replace
53
        $stringToReplace = '{{'.$entityProperty.'}}';
54
55
        //we provide a default value
56
        if ($attributeValue === null) {
57
            $attributeValue = '';
58
        }
59
60
        //we replace the string
61
        return str_replace($stringToReplace, $attributeValue, $url);
62
    }
63
}
64