Completed
Push — master ( 2c3b30...16a625 )
by Andrew
03:57
created

ClientController   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 13
dl 0
loc 174
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkAction() 0 13 1
C showAction() 0 48 11
A searchAction() 0 4 1
A homeAction() 0 21 4
B addAction() 0 37 6
A delAction() 0 13 2
1
<?php
2
3
namespace ClientBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
9
use ClientBundle\Form\Type\ClientForm;
10
use ClientBundle\Form\Type\CategoriesForm;
11
use ClientBundle\Entity\Client;
12
use ClientBundle\Entity\Contact;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * Class ClientController
17
 */
18
class ClientController extends Controller
19
{
20
    /**
21
     * @param int $id
22
     * @return \Symfony\Component\HttpFoundation\Response
23
     */
24
    public function checkAction($id)
25
    {
26
        $validator = $this->get('client.contains_checkhaveallcontacttypes_validator');
27
        $em = $this->getDoctrine()->getManager();
28
        $client = $em->find('ClientBundle:Client', $id);
29
        $allContactTypes = $em->getRepository('ClientBundle:ContactType')->findAll();
30
        $validator->setContactTypes($allContactTypes);
31
32
        $fuckingTmp = new \ClientBundle\Validator\Constraints\ContainsCheckHaveAllContactTypes();
33
        $errors = $validator->validate($client, $fuckingTmp);
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
35
        return new Response('test '.$id);
36
    }
37
38
    /**
39
     * @param int     $id
40
     * @param Request $request
41
     * @Method({"GET", "POST"})
42
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
43
     */
44
    public function showAction($id, Request $request)
45
    {
46
        if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
47
            throw $this->createAccessDeniedException();
48
        }
49
        $em = $this->getDoctrine()->getManager();
50
        $client = $em->find('ClientBundle:Client', $id);
51
52
        if (!$client) {
53
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
54
        } else {
55
            $form = $this->createForm(ClientForm::class, $client, array('id' => $client->getId()));
56
            // перенести в вьюв
57
            if ($this->isGranted('ROLE_ADMIN')) {
58
                $form->add('delete', SubmitType::class);
59
            }
60
61
            $form->handleRequest($request);
62
63
            if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
64
                $client = $form->getData();
65
66
                if ($this->isGranted('ROLE_ADMIN') && $form->get('delete')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

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...
67
                    return $this->redirectToRoute('client_del', array('id' => $client->getId()));
68
                } elseif ($form->get('addcontact')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

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...
69
                    $contact = new Contact();
70
                    $clientForm = $request->request->get('client_form');
71
72
                    $contactType = $em->find('ClientBundle:ContactType', $clientForm['newtypecontact']);
73
                    if (!$contactType) {
74
                        throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $clientForm['newtypecontact']));
75
                    } else {
76
                        $contact->setType($contactType);
77
                        $contact->setMean($clientForm['newmeancontact']);
78
                        $client->addContact($contact);
79
                        $em->persist($contact);
80
                        $em->flush();
81
                    }
82
                } else {
83
                    $em->flush();
84
85
                    return $this->redirectToRoute('client_home');
86
                }
87
            }
88
89
            return $this->render('ClientBundle:Default:edit.html.twig', ['form' => $form->createView(), 'client' => $client]);
90
        }
91
    }
92
93
    /**
94
     * @Method("GET")
95
     * @return \Symfony\Component\HttpFoundation\Response
96
     */
97
    public function searchAction()
98
    {
99
         return $this->render('ClientBundle:Default:home.html.twig');
100
    }
101
102
    /**
103
     * @param Request $request
104
     * @param string  $ctg
105
     * @Method({"GET", "POST"})
106
     * @return \Symfony\Component\HttpFoundation\Response
107
     */
108
    public function homeAction(Request $request, $ctg = '')
109
    {
110
        $ctgRequest = $request->request->get('categories_form');
111
        if (null !== $ctgRequest && '' !== $ctgRequest['categories']) {
112
            $ctg = (int) $ctgRequest['categories'];
113
        }
114
        $em = $this->getDoctrine()->getManager();
115
        $categories = $em->getRepository('ClientBundle:Category')->findAll();
116
        $form = $this->createForm(CategoriesForm::class, $categories);
117
        $form->handleRequest($request);
118
        if (is_integer($ctg)) {
119
            $category = $em->getRepository('ClientBundle:Category')->find($ctg);
120
            $clients =  $category->getClients();
121
122
            return $this->render('ClientBundle:Default:home.html.twig', ['form' => $form->createView(), 'clients' => $clients]);
123
        } else {
124
            $clients = $em->getRepository('ClientBundle:Client')->findAll();
125
126
            return $this->render('ClientBundle:Default:home.html.twig', ['form' => $form->createView(), 'clients' => $clients]);
127
        }
128
    }
129
130
    /**
131
     * @param Request $request
132
     * @Method({"GET", "POST"})
133
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
134
     */
135
    public function addAction(Request $request)
136
    {
137
        $em = $this->getDoctrine()->getManager();
138
        $client = new Client();
139
        $form = $this->createForm(ClientForm::class, $client, array('id' => $client->getId()));
140
141
        $form->handleRequest($request);
142
143
        if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
144
            $client = $form->getData();
145
146
            if ($form->get('addcontact')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not seem to exist on object<Symfony\Component\Form\Form>.

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...
147
                $contact = new Contact();
148
                $clientForm = $request->request->get('client_form');
149
150
                $contactType = $em->getRepository('ClientBundle:ContactType')->find($clientForm['newtypecontact']);
151
                if (!$contactType) {
152
                    throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $clientForm['newtypecontact']));
153
                } else {
154
                    $contact->setType($contactType);
155
                    $contact->setMean($clientForm['newmeancontact']);
156
                    $client->addContact($contact);
157
                    $em->persist($contact);
158
                    $em->persist($client);
159
                    $em->flush();
160
161
                    return $this->redirectToRoute('client_show', ['id' => $client->getId()]);
162
                }
163
            }
164
            $em->persist($client);
165
            $em->flush();
166
167
            return $this->redirectToRoute('client_home');
168
        }
169
170
        return $this->render('ClientBundle:Default:edit.html.twig', ['form' => $form->createView(), 'client' => $client]);
171
    }
172
173
    /**
174
     * @param int $id
175
     * @Method({"GET", "POST"})
176
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
177
     */
178
    public function delAction($id)
179
    {
180
        $em = $this->getDoctrine()->getManager();
181
        $client = $em->getRepository('ClientBundle:Client')->find($id);
182
        if (!$client) {
183
            throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $id));
184
        } else {
185
            $em->remove($client);
186
            $em->flush();
187
        }
188
189
        return $this->redirectToRoute('client_home');
190
    }
191
}
192