Completed
Push — master ( 35bf32...5024d1 )
by Karel
12s
created

hybridTransform()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0175
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Transformer;
13
14
use FOS\ElasticaBundle\HybridResult;
15
16
/**
17
 * Holds a collection of transformers for an index wide transformation.
18
 *
19
 * @author Tim Nagel <[email protected]>
20
 */
21
class ElasticaToModelTransformerCollection implements ElasticaToModelTransformerInterface
22
{
23
    /**
24
     * @var ElasticaToModelTransformerInterface[]
25
     */
26
    protected $transformers = [];
27
28
    /**
29
     * @param array $transformers
30
     */
31 6
    public function __construct(array $transformers)
32
    {
33 6
        $this->transformers = $transformers;
34 6
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getObjectClass()
40
    {
41 1
        return array_map(function (ElasticaToModelTransformerInterface $transformer) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_map(functio..., $this->transformers); (array) is incompatible with the return type declared by the interface FOS\ElasticaBundle\Trans...terface::getObjectClass of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42 1
            return $transformer->getObjectClass();
43 1
        }, $this->transformers);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getIdentifierField()
50
    {
51 1
        return array_map(function (ElasticaToModelTransformerInterface $transformer) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_map(functio..., $this->transformers); (array) is incompatible with the return type declared by the interface FOS\ElasticaBundle\Trans...ace::getIdentifierField of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52 1
            return $transformer->getIdentifierField();
53 1
        }, $this->transformers);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 4
    public function transform(array $elasticaObjects)
60
    {
61 4
        $sorted = [];
62 4
        foreach ($elasticaObjects as $object) {
63 4
            $sorted[$object->getType()][] = $object;
64
        }
65
66 4
        $transformed = [];
67 4
        foreach ($sorted as $type => $objects) {
68 4
            $transformedObjects = $this->transformers[$type]->transform($objects);
69 4
            $identifierGetter = 'get'.ucfirst($this->transformers[$type]->getIdentifierField());
70 4
            $transformed[$type] = array_combine(
71 4
                array_map(
72 4
                    function ($o) use ($identifierGetter) {
73 4
                        return $o->$identifierGetter();
74 4
                    },
75
                    $transformedObjects
76
                ),
77
                $transformedObjects
78
            );
79
        }
80
81 4
        $result = [];
82 4
        foreach ($elasticaObjects as $object) {
83 4
            if (array_key_exists((string) $object->getId(), $transformed[$object->getType()])) {
84 4
                $result[] = $transformed[$object->getType()][(string) $object->getId()];
85
            }
86
        }
87
88 4
        return $result;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function hybridTransform(array $elasticaObjects)
95
    {
96 1
        $objects = $this->transform($elasticaObjects);
97
98 1
        $result = [];
99 1
        for ($i = 0, $j = count($elasticaObjects); $i < $j; ++$i) {
100 1
            if (!isset($objects[$i])) {
101
                continue;
102
            }
103 1
            $result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
104
        }
105
106 1
        return $result;
107
    }
108
}
109