getEventAssociation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the module FeatureType                                  */
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 FeatureType\Controller;
10
11
use FeatureType\Model\FeatureTypeQuery;
12
use Thelia\Core\HttpFoundation\Response;
13
use FeatureType\Event\FeatureTypeEvents;
14
use FeatureType\Event\FeatureTypeEvent;
15
use Thelia\Core\Security\AccessManager;
16
use Thelia\Core\Security\Resource\AdminResources;
17
use Thelia\Model\FeatureQuery;
18
use Thelia\Core\Translation\Translator;
19
use FeatureType\FeatureType as FeatureTypeCore;
20
21
/**
22
 * Class FeatureTypeFeatureController
23
 * @package FeatureType\Controller
24
 * @author Gilles Bourgeat <[email protected]>
25
 */
26
class FeatureTypeFeatureController extends FeatureTypeController
27
{
28
    /**
29
     * @param int $feature_type_id
30
     * @param int $feature_id
31
     * @return Response
32
     */
33
    public function associateAction($feature_type_id, $feature_id)
34
    {
35
        if (null !== $response = $this->checkAuth(array(AdminResources::FEATURE), null, AccessManager::UPDATE)) {
36
            return $response;
37
        }
38
39
        $form = $this->createForm('feature_type.associate');
40
41
        try {
42
            $this->validateForm($form, 'POST');
43
44
            $this->dispatch(
45
                FeatureTypeEvents::FEATURE_TYPE_ASSOCIATE,
46
                $this->getEventAssociation($feature_type_id, $feature_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->viewFeature($feature_id);
58
        }
59
    }
60
61
    /**
62
     * @param int $feature_type_id
63
     * @param int $feature_id
64
     * @return Response
65
     */
66
    public function dissociateAction($feature_type_id, $feature_id)
67
    {
68
        if (null !== $response = $this->checkAuth(array(AdminResources::FEATURE), null, AccessManager::UPDATE)) {
69
            return $response;
70
        }
71
72
        $form = $this->createForm('feature_type.dissociate');
73
74
        try {
75
            $this->validateForm($form, 'POST');
76
77
            $this->dispatch(
78
                FeatureTypeEvents::FEATURE_TYPE_DISSOCIATE,
79
                $this->getEventAssociation($feature_type_id, $feature_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->viewFeature($feature_id);
91
        }
92
    }
93
94
    /**
95
     * @param int $feature_type_id
96
     * @param int $feature_id
97
     * @return FeatureTypeEvent
98
     * @throws \Exception
99
     */
100
    protected function getEventAssociation($feature_type_id, $feature_id)
101
    {
102
        if (null === $feature = FeatureQuery::create()->findPk($feature_id)) {
103
            throw new \Exception(Translator::getInstance()->trans(
104
                "Feature not found",
105
                array(),
106
                FeatureTypeCore::MODULE_DOMAIN
107
            ));
108
        }
109
110
        if (null === $featureType = FeatureTypeQuery::create()->findPk($feature_type_id)) {
111
            throw new \Exception(Translator::getInstance()->trans(
112
                "Feature type not found",
113
                array(),
114
                FeatureTypeCore::MODULE_DOMAIN
115
            ));
116
        }
117
118
        $event = new FeatureTypeEvent($featureType);
119
        $event->setFeature($feature);
120
121
        return $event;
122
    }
123
}
124