Completed
Push — master ( 3560db...6e658b )
by
unknown
08:14
created

ContactPhoneController::getFormHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\Controller\Api\Rest;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
8
use FOS\RestBundle\Controller\Annotations\NamePrefix;
9
use FOS\RestBundle\Controller\Annotations\RouteResource;
10
use FOS\RestBundle\Routing\ClassResourceInterface;
11
use FOS\RestBundle\Util\Codes;
12
13
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
14
15
use Oro\Bundle\SecurityBundle\Annotation\Acl;
16
use Oro\Bundle\FormBundle\Form\Handler\ApiFormHandler;
17
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
18
use Oro\Bundle\SoapBundle\Controller\Api\Rest\RestController;
19
20
use OroCRM\Bundle\ContactBundle\Entity\Contact;
21
22
/**
23
 * @RouteResource("phone")
24
 * @NamePrefix("oro_api_")
25
 */
26
class ContactPhoneController extends RestController implements ClassResourceInterface
27
{
28
    /**
29
     * REST GET list
30
     *
31
     * @ApiDoc(
32
     *      description="Get all phones items",
33
     *      resource=true
34
     * )
35
     * @AclAncestor("orocrm_contact_view")
36
     * @param int $contactId
37
     * @return Response
38
     */
39 View Code Duplication
    public function cgetAction($contactId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        /** @var Contact $contact */
42
        $contact = $this->getContactManager()->find($contactId);
43
        $result = [];
44
        if (!empty($contact)) {
45
            $items = $contact->getPhones();
46
47
            foreach ($items as $item) {
48
                $result[] = $this->getPreparedItem($item);
49
            }
50
        }
51
52
        return new JsonResponse(
53
            $result,
54
            empty($contact) ? Codes::HTTP_NOT_FOUND : Codes::HTTP_OK
55
        );
56
    }
57
58
    /**
59
     * REST GET primary phone
60
     *
61
     * @param string $contactId
62
     *
63
     * @ApiDoc(
64
     *      description="Get contact primary phone",
65
     *      resource=true
66
     * )
67
     * @AclAncestor("orocrm_contact_view")
68
     * @return Response
69
     */
70 View Code Duplication
    public function getPrimaryAction($contactId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        /** @var Contact $contact */
73
        $contact = $this->getContactManager()->find($contactId);
74
75
        if ($contact) {
76
            $phone = $contact->getPrimaryPhone();
77
        } else {
78
            $phone = null;
79
        }
80
81
        $responseData = $phone ? json_encode($this->getPreparedItem($phone)) : '';
82
83
        return new Response($responseData, $phone ? Codes::HTTP_OK : Codes::HTTP_NOT_FOUND);
84
    }
85
86
    /**
87
     * Create entity ContactPhone
88
     * oro_api_post_contact_phone
89
     *
90
     * @return Response
91
     *
92
     * @ApiDoc(
93
     *      description="Create entity",
94
     *      resource=true,
95
     *      requirements = {
96
     *          {"name"="id", "dataType"="integer"},
97
     *      }
98
     * )
99
     */
100
    public function postAction()
101
    {
102
        $response = $this->handleCreateRequest();
103
104
        return $response;
105
    }
106
107
    /**
108
     * Delete entity ContactPhone
109
     * oro_api_delete_contact_phone
110
     *
111
     * @param int $id
112
     *
113
     * @ApiDoc(
114
     *      description="Delete ContactPhone"
115
     * )
116
     *
117
     * @return Response
118
     */
119 View Code Duplication
    public function deleteAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        try {
122
            $this->getDeleteHandler()->handleDelete($id, $this->getManager());
123
124
            return new JsonResponse(["id" => ""]);
125
        } catch (\Exception $e) {
126
            return new JsonResponse(["code" => $e->getCode(), "message"=>$e->getMessage() ], $e->getCode());
127
        }
128
    }
129
130
    protected function getContactManager()
131
    {
132
        return $this->get('orocrm_contact.contact.manager.api');
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getManager()
139
    {
140
        return $this->get('orocrm_contact.contact_phone.manager.api');
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    protected function getPreparedItem($entity, $resultFields = [])
147
    {
148
        $result['id']      = $entity->getId();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
149
        $result['owner']   = (string) $entity->getOwner();
150
        $result['phone']   = $entity->getPhone();
151
        $result['primary'] = $entity->isPrimary();
152
153
        return $result;
154
    }
155
156
    /**
157
     * @return ApiFormHandler
158
     */
159
    public function getFormHandler()
160
    {
161
        return $this->get('orocrm_contact.form.type.contact_phone.handler');
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getForm()
168
    {
169
        return $this->get('orocrm_contact.form.type.contact_phone.type');
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getDeleteHandler()
176
    {
177
        return $this->get('orocrm_contact.form.type.contact_phone.handler');
178
    }
179
}
180