Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

Assembler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 49
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A prepare() 0 8 3
A assemble() 0 15 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity;
4
5
use AmaTeam\ElasticSearch\API\Entity;
6
use AmaTeam\ElasticSearch\API\Entity\AssemblerInterface;
7
use AmaTeam\ElasticSearch\API\Entity\Descriptor\Hierarchy\CompilerInterface;
8
use AmaTeam\ElasticSearch\API\Entity\Descriptor\Hierarchy\BuilderInterface;
9
use AmaTeam\ElasticSearch\API\Entity\Descriptor\ManagerInterface;
10
use AmaTeam\ElasticSearch\API\Entity\DescriptorInterface;
11
use AmaTeam\ElasticSearch\API\Entity\Mapping\Conversion\DefaultContext;
12
use AmaTeam\ElasticSearch\API\Entity\Mapping\ConverterInterface;
13
use AmaTeam\ElasticSearch\API\EntityInterface;
14
use AmaTeam\ElasticSearch\Entity\Descriptor\Compiler;
15
use AmaTeam\ElasticSearch\Entity\Descriptor\Hierarchy\Builder;
16
use AmaTeam\ElasticSearch\Entity\Descriptor\Operations;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AmaTeam\ElasticSearch\Entity\Operations. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use AmaTeam\ElasticSearch\Entity\Mapping\Converter;
18
19
class Assembler implements AssemblerInterface
20
{
21
    /**
22
     * @var BuilderInterface
23
     */
24
    private $hierarchyBuilder;
25
    /**
26
     * @var CompilerInterface
27
     */
28
    private $compiler;
29
    /**
30
     * @var ConverterInterface
31
     */
32
    private $converter;
33
34
    /**
35
     * @param ManagerInterface $manager
36
     */
37
    public function __construct(ManagerInterface $manager) {
38
        $this->hierarchyBuilder = new Builder($manager);
39
        $this->compiler = new Compiler();
40
        $this->converter = new Converter($manager);
41
    }
42
43
    public function assemble(DescriptorInterface $descriptor): EntityInterface
44
    {
45
        $copy = $this->prepare($descriptor);
46
        $hierarchy = $this->hierarchyBuilder->build($copy);
47
        $compiled = $this->compiler->compile($hierarchy);
48
        $context = (new DefaultContext())->setRootLevelMapping(true);
49
        $mapping = $this->converter->convert($compiled->getMapping(), $context);
50
        return (new Entity($descriptor->getName()))
51
            ->setParentNames($compiled->getParentNames())
52
            ->setRootLevelDocument($compiled->isRootLevelDocument())
53
            ->setOriginalDescriptor(Operations::from($descriptor))
54
            ->setCompiledDescriptor($compiled)
55
            ->setHierarchy($hierarchy)
56
            ->setMapping($mapping)
57
            ->setIndexing($compiled->getIndexing());
58
    }
59
60
    private function prepare(DescriptorInterface $descriptor): DescriptorInterface
61
    {
62
        $copy = Operations::from($descriptor);
63
        if ($copy->getParentNames() === null) {
64
            $parent = get_parent_class($descriptor->getName());
65
            $copy->setParentNames($parent ? [$parent] : []);
66
        }
67
        return $copy;
68
    }
69
}
70