AttributeTypeAttributeController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A associateAction() 0 27 3
A dissociateAction() 0 27 3
A getEventAssociation() 0 23 3
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the module AttributeType                                */
4
/*                                                                                   */
5
/*      For the full copyright and license information, please view the LICENSE.txt  */
6
/*      file that was distributed with this source code.                             */
7
/*************************************************************************************/
8
9
namespace AttributeType\Controller;
10
11
use AttributeType\Model\AttributeTypeQuery;
12
use Thelia\Core\HttpFoundation\Response;
13
use AttributeType\Event\AttributeTypeEvents;
14
use AttributeType\Event\AttributeTypeEvent;
15
use Thelia\Core\Security\AccessManager;
16
use Thelia\Core\Security\Resource\AdminResources;
17
use Thelia\Model\AttributeQuery;
18
use Thelia\Core\Translation\Translator;
19
use AttributeType\AttributeType as AttributeTypeCore;
20
21
/**
22
 * Class AttributeTypeAttributeController
23
 * @package AttributeType\Controller
24
 * @author Gilles Bourgeat <[email protected]>
25
 */
26
class AttributeTypeAttributeController extends AttributeTypeController
27
{
28
    /**
29
     * @param int $attribute_type_id
30
     * @param int $attribute_id
31
     * @return Response
32
     */
33
    public function associateAction($attribute_type_id, $attribute_id)
34
    {
35
        if (null !== $response = $this->checkAuth(array(AdminResources::ATTRIBUTE), null, AccessManager::UPDATE)) {
36
            return $response;
37
        }
38
39
        $form = $this->createForm('attribute_type.associate');
40
41
        try {
42
            $this->validateForm($form, 'POST');
43
44
            $this->dispatch(
45
                AttributeTypeEvents::ATTRIBUTE_TYPE_ASSOCIATE,
46
                $this->getEventAssociation($attribute_type_id, $attribute_id)
47
            );
48
49
            return $this->generateSuccessRedirect($form);
50
        } catch (\Exception $e) {
51
            $this->setupFormErrorContext(
52
                $this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)),
53
                $e->getMessage(),
54
                $form
55
            );
56
57
            return $this->viewAttribute($attribute_id);
58
        }
59
    }
60
61
    /**
62
     * @param int $attribute_type_id
63
     * @param int $attribute_id
64
     * @return Response
65
     */
66
    public function dissociateAction($attribute_type_id, $attribute_id)
67
    {
68
        if (null !== $response = $this->checkAuth(array(AdminResources::ATTRIBUTE), null, AccessManager::UPDATE)) {
69
            return $response;
70
        }
71
72
        $form = $this->createForm('attribute_type.dissociate');
73
74
        try {
75
            $this->validateForm($form, 'POST');
76
77
            $this->dispatch(
78
                AttributeTypeEvents::ATTRIBUTE_TYPE_DISSOCIATE,
79
                $this->getEventAssociation($attribute_type_id, $attribute_id)
80
            );
81
82
            return $this->generateSuccessRedirect($form);
83
        } catch (\Exception $e) {
84
            $this->setupFormErrorContext(
85
                $this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)),
86
                $e->getMessage(),
87
                $form
88
            );
89
90
            return $this->viewAttribute($attribute_id);
91
        }
92
    }
93
94
    /**
95
     * @param int $attribute_type_id
96
     * @param int $attribute_id
97
     * @return AttributeTypeEvent
98
     * @throws \Exception
99
     */
100
    protected function getEventAssociation($attribute_type_id, $attribute_id)
101
    {
102
        if (null === $attribute = AttributeQuery::create()->findPk($attribute_id)) {
103
            throw new \Exception(Translator::getInstance()->trans(
104
                "Attribute not found",
105
                array(),
106
                AttributeTypeCore::MODULE_DOMAIN
107
            ));
108
        }
109
110
        if (null === $attributeType = AttributeTypeQuery::create()->findPk($attribute_type_id)) {
111
            throw new \Exception(Translator::getInstance()->trans(
112
                "Attribute type not found",
113
                array(),
114
                AttributeTypeCore::MODULE_DOMAIN
115
            ));
116
        }
117
118
        $event = new AttributeTypeEvent($attributeType);
119
        $event->setAttribute($attribute);
120
121
        return $event;
122
    }
123
}
124