Completed
Push — master ( 07ded0...750096 )
by Laurent
03:19
created

ControllerHelper::setOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 4
1
<?php
2
/**
3
 * ControllerHelper Helpers des Controller de l'application GLSR.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Helper;
16
17
use AppBundle\Entity\Article;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
21
/**
22
 * Controller helper.
23
 *
24
 * @category Helper
25
 */
26
class ControllerHelper
27
{
28
    public $translator;
29
    public $session;
30
    public function __construct($translator, $session)
31
    {
32
        $this->translator = $translator;
33
        $this->session = $session;
34
    }
35
36
    /**
37
     * Get the entity
38
     *
39
     * @param string $entityName Name of Entity
40
     * @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances
41
     * @return array|\Doctrine\ORM\QueryBuilder|null Entity elements
42
     */
43
    protected function getEntity($entityName, $etm)
44
    {
45
        $roles = ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'];
46
        switch ($entityName) {
47
            case 'Article':
48
            case 'Supplier':
49
                if ($this->getUser() !== null && in_array($this->getUser()->getRoles()[0], $roles)) {
0 ignored issues
show
Bug introduced by
The method getUser() does not seem to exist on object<AppBundle\Helper\ControllerHelper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
                    $entities = $etm->getRepository('AppBundle:'.$entityName)->getAllItems();
51
                } else {
52
                    $entities = $etm->getRepository('AppBundle:'.$entityName)->getItems();
53
                }
54
                break;
55
            case 'User':
56
                $entities = $etm->getRepository('AppBundle:'.$entityName)->getUsers();
57
                break;
58
            case 'FamilyLog':
59
                $entities = $etm->getRepository('AppBundle:'.$entityName)->childrenHierarchy();
60
                break;
61
            case 'UnitStorage':
62
                $entities = $etm->getRepository('AppBundle:'.$entityName)->createQueryBuilder('u');
63
                break;
64
            default:
65
                $entities = $etm->getRepository('AppBundle:'.$entityName)->findAll();
66
        }
67
        return $entities;
68
    }
69
70
    /**
71
     * Tests of creation conditions.
72
     *
73
     * @param array Articles à tester
74
     * @return boolean
75
     */
76
    public function hasSupplierArticles(array $articles)
77
    {
78
        $return = true;
79
80
        // This supplier has no articles!
81
        if (count($articles) < 1) {
82
            $message = $this->translator->trans('settings.no_articles', array(), 'gs_suppliers');
83
            $this->session->getFlashBag()->add('danger', $message);
84
            $return = false;
85
        }
86
87
        return $return;
88
    }
89
90
    /**
91
     * Tests Order in progress for a supplier.
92
     *
93
     * @param \AppBundle\Entity\Article $articles Articles to test
94
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Named object manager
95
     * @return boolean
96
     */
97
    public function isOrderInProgress(Article $articles, ObjectManager $etm)
98
    {
99
        $return = false;
100
        $orders = $etm->getRepository('AppBundle:Orders')->findAll();
101
        // This provider already has an order in progress!
102
        foreach ($orders as $order) {
103
            if ($order->getSupplier() === $articles->getSupplier()) {
104
                $return = true;
105
            }
106
        }
107
        return $return;
108
    }
109
110
    /**
111
     * Array of file (`pdf`) layout.
112
     *
113
     * @param string $date  File date
114
     * @param string $title File title
115
     * @return array
116
     */
117
    protected function getArray($date, $title)
118
    {
119
        $array = array(
120
            'margin-top' => 15,
121
            'header-spacing' => 5,
122
            'header-font-size' => 8,
123
            'header-left' => 'G.L.S.R.',
124
            'header-center' => $title,
125
            'header-right' => $date,
126
            'header-line' => true,
127
            'margin-bottom' => 15,
128
            'footer-spacing' => 5,
129
            'footer-font-size' => 8,
130
            'footer-left' => 'GLSR &copy 2014 and beyond.',
131
            'footer-right' => 'Page [page]/[toPage]',
132
            'footer-line' => true,
133
        );
134
        return $array;
135
    }
136
137
    /**
138
     * test paramters to return
139
     *
140
     * @param Object $entity     Entity to return
141
     * @param string $entityName Entity name to test
142
     * @return array             Parameters to return
143
     */
144
    protected function testReturnParam($entity, $entityName)
145
    {
146
        $entityArray = ['company', 'settings', 'group', 'tva'];
147
        if (in_array($entityName, $entityArray, true)) {
148
            $param = ['id' => $entity->getId()];
149
        } else {
150
            $param = ['slug' => $entity->getSlug()];
151
        }
152
153
        return $param;
154
    }
155
156
    /**
157
     * SetOrder for the SortAction in views.
158
     *
159
     * @param string $name   session name
160
     * @param string $entity entity name
161
     * @param string $field  field name
162
     * @param string $type   sort type ("ASC"/"DESC")
163
     */
164
    protected function setOrder($name, $entity, $field, $type = 'ASC')
165
    {
166
        $session = new Session();
167
168
        $session->set('sort.'.$name, array('entity' => $entity, 'field' => $field, 'type' => $type));
169
    }
170
171
    /**
172
     * GetOrder for the SortAction in views.
173
     *
174
     * @param string $name session name
175
     *
176
     * @return array
177
     */
178
    protected function getOrder($name)
179
    {
180
        $session = new Session();
181
182
        return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null;
183
    }
184
}
185