Passed
Branch master (a0cc06)
by Dāvis
17:06
created

PositionHandler::getPosition()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 20

Duplication

Lines 10
Ratio 31.25 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 9
nop 3
dl 10
loc 32
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Position\Service;
4
5
use Doctrine\Common\Util\ClassUtils;
6
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class PositionHandler
9
{
10
    protected $positionField;
11
12
    /**
13
     * @var EntityManager
0 ignored issues
show
Bug introduced by
The type Sludio\HelperBundle\Position\Service\EntityManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
     */
15
    protected $em;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
16
17
    public function __construct(EntityManagerInterface $entityManager)
18
    {
19
        $this->em = $entityManager;
20
    }
21
22
    public function getLastPosition($entity)
23
    {
24
        $query = $this->em->createQuery(sprintf('SELECT MAX(m.%s) FROM %s m', $positionFiles = $this->getPositionFieldByEntity($entity), $entity));
25
        $result = $query->getResult();
26
27
        if (array_key_exists(0, $result)) {
28
            return intval($result[0][1]);
29
        }
30
31
        return 0;
32
    }
33
34
    /**
35
     * @param mixed $positionField
36
     */
37
    public function setPositionField($positionField)
38
    {
39
        $this->positionField = $positionField;
40
    }
41
42
    /**
43
     * @param $entity
44
     *
45
     * @return string
46
     */
47
    public function getPositionFieldByEntity($entity)
48
    {
49
        if (is_object($entity)) {
50
            $entity = ClassUtils::getClass($entity);
51
        }
52
        if (isset($this->positionField['entities'][$entity])) {
53
            return $this->positionField['entities'][$entity];
54
        } else {
55
            return $this->positionField['default'];
56
        }
57
    }
58
59
    /**
60
     * @param $object
61
     * @param $position
62
     * @param $lastPosition
63
     *
64
     * @return int
65
     */
66
    public function getPosition($object, $position, $lastPosition)
67
    {
68
        $getter = sprintf('get%s', ucfirst($this->getPositionFieldByEntity($object)));
69
        $newPosition = 0;
70
71
        switch ($position) {
72 View Code Duplication
            case 'up':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
                if ($object->{$getter}() > 0) {
74
                    $newPosition = $object->{$getter}() - 1;
75
                }
76
                break;
77
78 View Code Duplication
            case 'down':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
                if ($object->{$getter}() < $lastPosition) {
80
                    $newPosition = $object->{$getter}() + 1;
81
                }
82
                break;
83
84
            case 'top':
85
                if ($object->{$getter}() > 0) {
86
                    $newPosition = 0;
87
                }
88
                break;
89
90
            case 'bottom':
91
                if ($object->{$getter}() < $lastPosition) {
92
                    $newPosition = $lastPosition;
93
                }
94
                break;
95
        }
96
97
        return $newPosition;
98
    }
99
}
100