PositionHandler   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setPositionField() 0 3 1
A sludioUp() 0 7 2
A sludioBottom() 0 7 2
A sludioTop() 0 7 2
A getLastPosition() 0 10 2
A sludioDown() 0 7 2
A getPosition() 0 5 1
A getPositionFieldByEntity() 0 10 3
1
<?php
2
3
namespace Sludio\HelperBundle\Position\Service;
4
5
use Doctrine\Common\Util\ClassUtils;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Sludio\HelperBundle\Position\Component\PositionInterface;
8
9
class PositionHandler
10
{
11
    protected $positionField;
12
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    protected $entityManager;
17
18
    public function __construct(EntityManagerInterface $entityManager)
19
    {
20
        $this->entityManager = $entityManager;
21
    }
22
23
    public function getLastPosition($entity)
24
    {
25
        $query = $this->entityManager->createQuery(sprintf('SELECT MAX(m.%s) FROM %s m', $this->getPositionFieldByEntity($entity), $entity));
26
        $result = $query->getResult();
27
28
        if (array_key_exists(0, $result)) {
29
            return (int)$result[0][1];
30
        }
31
32
        return 0;
33
    }
34
35
    /**
36
     * @param $entity
37
     *
38
     * @return string
39
     */
40
    public function getPositionFieldByEntity($entity)
41
    {
42
        if (\is_object($entity)) {
43
            $entity = ClassUtils::getClass($entity);
44
        }
45
        if (isset($this->positionField['entities'][$entity])) {
46
            return $this->positionField['entities'][$entity];
47
        }
48
49
        return $this->positionField['default'];
50
    }
51
52
    /**
53
     * @param mixed $positionField
54
     */
55
    public function setPositionField($positionField)
56
    {
57
        $this->positionField = $positionField;
58
    }
59
60
    /**
61
     * @param $entity
62
     * @param $position
63
     * @param $lastPosition
64
     *
65
     * @return int
66
     */
67
    public function getPosition(PositionInterface $entity, $position, $lastPosition)
68
    {
69
        $getter = sprintf('get%s', ucfirst($this->getPositionFieldByEntity($entity)));
70
71
        return (int)$this->{'sludio'.ucfirst($position)}($entity->{$getter}(), $lastPosition);
72
    }
73
74
    protected function sludioUp($actual)
75
    {
76
        if ($actual > 0) {
77
            return $actual - 1;
78
        }
79
80
        return $actual;
81
    }
82
83
    protected function sludioDown($actual, $last)
84
    {
85
        if ($actual < $last) {
86
            return $actual + 1;
87
        }
88
89
        return $actual;
90
    }
91
92
    protected function sludioTop($actual)
93
    {
94
        if ($actual > 0) {
95
            return 0;
96
        }
97
98
        return $actual;
99
    }
100
101
    protected function sludioBottom($actual, $last)
102
    {
103
        if ($actual < $last) {
104
            return $last;
105
        }
106
107
        return $actual;
108
    }
109
}
110