EmojiController   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 169
Duplicated Lines 17.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 15
Bugs 0 Features 2
Metric Value
wmc 30
c 15
b 0
f 2
lcom 1
cbo 4
dl 30
loc 169
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAllEmoji() 15 15 3
A findEmoji() 15 15 3
C insertEmoji() 0 32 7
D updateEmoji() 0 30 9
C deleteEmoji() 0 23 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Emojinaija is a rest API service that provide access to
5
 * unlimited emoji images
6
 *
7
 * @package Ibonly\NaijaEmoji\EmojiController
8
 * @author  Ibraheem ADENIYI <[email protected]>
9
 * @license MIT <https://opensource.org/licenses/MIT>
10
 */
11
12
namespace Ibonly\NaijaEmoji;
13
14
use Slim\Slim;
15
use Ibonly\NaijaEmoji\Emoji;
16
use Firebase\JWT\ExpiredException;
17
use Ibonly\NaijaEmoji\EmojiInterface;
18
use Ibonly\NaijaEmoji\AuthController;
19
use Ibonly\PotatoORM\DataNotFoundException;
20
use Ibonly\PotatoORM\EmptyDatabaseException;
21
use Ibonly\NaijaEmoji\ProvideTokenException;
22
use Ibonly\NaijaEmoji\InvalidTokenException;
23
use Ibonly\PotatoORM\DataAlreadyExistException;
24
25
class EmojiController implements EmojiInterface
26
{
27
    protected $dataName;
28
    protected $auth;
29
30
    public function __construct ()
31
    {
32
        $this->dataName = new Emoji();
33
        $this->auth = new AuthController();
34
    }
35
36
    /**
37
     * getAllEmoji Get all the emoji's available
38
     *
39
     * @param  $app
40
     *
41
     * @return json
42
     */
43 View Code Duplication
    public function getAllEmoji (Slim $app)
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...
44
    {
45
        $app->response->headers->set('Content-Type', 'application/json');
46
        try
47
        {
48
            $data = $this->dataName->getAll()->toJson();
49
            $newData = json_decode($data);
50
            foreach ( $newData as $key ) {
51
                $key->keywords = explode(", ", $key->keywords);
52
            }
53
            return json_encode($newData);
54
        } catch ( EmptyDatabaseException $e ) {
55
            $app->halt(204, json_encode(['Message' => 'No content']));
56
        }
57
    }
58
59
    /**
60
     * findEmoji Find a particular emoji
61
     *
62
     * @param  $id
63
     * @param  $app
64
     *
65
     * @return json
66
     */
67 View Code Duplication
    public function findEmoji ($id, Slim $app)
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...
68
    {
69
        $app->response->headers->set('Content-Type', 'application/json');
70
        try
71
        {
72
            $data =  $this->dataName->where(['id' => $id])->toJson();
73
            $newData = json_decode($data);
74
            foreach ( $newData as $key ) {
75
                $key->keywords = explode(", ", $key->keywords);
76
            }
77
            return json_encode($newData);
78
        } catch ( DataNotFoundException $e ) {
79
            $app->halt(404, json_encode(['Message' => 'Not Found']));
80
        }
81
    }
82
83
    /**
84
     * insertEmoji Insert new emoji
85
     *
86
     * @param  $app
87
     *
88
     * @return json
89
     */
90
    public function insertEmoji (Slim $app)
91
    {
92
        $app->response->headers->set('Content-Type', 'application/json');
93
        $tokenData = $app->request->headers->get('Authorization');
94
        try
95
        {
96
            if ( ! isset( $tokenData ) )
97
                throw new ProvideTokenException();
98
99
            $data = $this->auth->authorizationDecode($tokenData);
100
            $this->dataName->id = NULL;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
101
            $this->dataName->name = $app->request->params('name');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
102
            $this->dataName->char = $app->request->params('char');
0 ignored issues
show
Bug introduced by
The property char does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
103
            $this->dataName->keywords = $app->request->params('keywords');
0 ignored issues
show
Bug introduced by
The property keywords does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
104
            $this->dataName->category = $app->request->params('category');
0 ignored issues
show
Bug introduced by
The property category does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
            $this->dataName->date_created = date('Y-m-d G:i:s');
0 ignored issues
show
Bug introduced by
The property date_created does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
106
            $this->dataName->date_modified = date('Y-m-d G:i:s');
0 ignored issues
show
Bug introduced by
The property date_modified does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
107
            $this->dataName->created_by = $data->user;
0 ignored issues
show
Bug introduced by
The property created_by does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
109
            $save = $this->dataName->save();
110
            if ( $save )
111
                $app->halt(200, json_encode(['Message' => 'Success']));
112
        } catch ( ExpiredException $e ) {
113
            $app->halt(401, json_encode(['Message' => 'Token has expired']));
114
        } catch ( DataAlreadyExistException $e ) {
115
            $app->halt(202, json_encode(['Message' => 'Not Created']));
116
        } catch ( InvalidTokenException $e ) {
117
            $app->halt(405, json_encode(['Message' => 'Invalid Token']));
118
        } catch ( ProvideTokenException $e ) {
119
            $app->halt(406, json_encode(['Message' => 'Enter a valid Token']));
120
        }
121
    }
122
123
    /**
124
     * updateEmoji update emoji details
125
     *
126
     * @param  $id
127
     * @param  $app
128
     *
129
     * @return json
130
     */
131
    public function updateEmoji ($id, Slim $app)
132
    {
133
        $app->response->headers->set('Content-Type', 'application/json');
134
        $tokenData = $app->request->headers->get('Authorization');
135
        try
136
        {
137
            if ( ! isset($tokenData) )
138
                throw new ProvideTokenException();
139
140
            $find = Emoji::find($id);
141
            $this->auth->authorizationDecode($tokenData);
142
            $fields = $app->request->isPut() ? $app->request->put() : $app->request->patch();
143
            foreach ( $fields as $key => $value )
144
            {
145
                $find->$key = $value;
146
            }
147
            $find->date_modified = date('Y-m-d G:i:s');
0 ignored issues
show
Bug introduced by
The property date_modified does not seem to exist in Ibonly\NaijaEmoji\Emoji.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
148
            $update = $find->update();
149
            if( $update )
150
                $app->halt(200, json_encode(['Message' => 'Emoji Updated Successfully']));
151
        } catch ( ExpiredException $e ) {
152
            $app->halt(401, json_encode(['Message' => 'Token has expired']));
153
        } catch ( DataNotFoundException $e ) {
154
            $app->halt(303, json_encode(['Message' => 'Invalid Credential supplied']));
155
        } catch ( InvalidTokenException $e ) {
156
            $app->halt(405, json_encode(['Message' => 'Invalid Token']));
157
        } catch ( ProvideTokenException $e ) {
158
            $app->halt(406, json_encode(['Message' => 'Enter a valid Token']));
159
        }
160
    }
161
162
    /**
163
     * deleteEmoji Delete already existing emoji
164
     *
165
     * @param  $id
166
     * @param  $app
167
     *
168
     * @return json
169
     */
170
    public function deleteEmoji ($id, Slim $app)
171
    {
172
        $app->response->headers->set('Content-Type', 'application/json');
173
        $tokenData = $app->request->headers->get('Authorization');
174
        try
175
        {
176
            if ( ! isset( $tokenData ) )
177
                throw new ProvideTokenException();
178
179
            $this->auth->authorizationDecode($tokenData);
180
            $deleted = $this->dataName->destroy($id);
181
            if ( $deleted )
182
                $app->halt(200, json_encode(['Message' => 'Emoji Deleted']));
183
        } catch ( ExpiredException $e ) {
184
            $app->halt(401, json_encode(['Message' => 'Token has expired']));
185
        } catch ( InvalidTokenException $e ) {
186
            $app->halt(405, json_encode(['Message' => 'Invalid Token']));
187
        } catch ( ProvideTokenException $e ) {
188
            $app->halt(406, json_encode(['Message' => 'Enter a valid Token']));
189
        } catch ( DataNotFoundException $e ) {
190
            $app->halt(401, json_encode(['Message' => 'Emoji not found']));
191
        }
192
    }
193
}