Completed
Pull Request — master (#812)
by Paul
06:29
created

ParameterConverter::setBusinessPropertyInstance()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A ParameterConverter::convertFromEntity() 0 15 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
    /**
15
     * Replace the code string with the value of the entity attribute.
16
     *
17
     * @param string           $string
18
     * @param BusinessProperty $businessProperty
19
     * @param object           $entity
20
     *
21
     * @throws \Exception
22
     *
23
     * @return string The updated string
24
     */
25
    public function convertFromEntity($string, BusinessProperty $businessProperty, $entity)
26
    {
27
        //test parameters
28
        if ($entity === null) {
29
            throw new \Exception('The parameter entity can not be null');
30
        }
31
        //the attribute to set
32
        $entityProperty = $businessProperty->getName();
33
34
        //the value of the attribute
35
        $accessor = new PropertyAccessor();
36
        $attributeValue = $accessor->getValue($entity, $entityProperty);
37
38
        return $this->convert($string, 'item.'.$entityProperty, $attributeValue);
39
    }
40
41
    /**
42
     * Replace the code string with the value of the entity attribute.
43
     *
44
     * @param string $string
45
     * @param string $entityProperty
46
     * @param string $attributeValue
47
     *
48
     * @throws \Exception
49
     *
50
     * @return string The updated string
51
     */
52
    public function convert($string, $entityProperty, $attributeValue)
53
    {
54
        //the string to replace
55
        $stringToReplace = '{{'.$entityProperty.'}}';
56
57
        //we provide a default value
58
        if ($attributeValue === null) {
59
            $attributeValue = '';
60
        }
61
62
        //we replace the string
63
        return str_replace($stringToReplace, $attributeValue, $string);
64
    }
65
}
66