Completed
Pull Request — master (#24)
by
unknown
05:45 queued 03:36
created

UserStatistics::getStatsBySegmentType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\Model\Model;
7
8
/**
9
 * Iterable collection of statistics resources.
10
 */
11
class UserStatistics extends Model
12
{
13
    /**
14
     * Get the user's statistics.
15
     *
16
     * @return Array of Statistics.
17
     */
18
    public function getStatistics()
19
    {
20
        $stats = array();
21
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
22
                array_push($stats,$statistic);
23
        }
24
        return $stats;
25
    }
26
27
    /**
28
    * Get a statistic.
29
    *
30
    * @param $typeCode code of the category type.
31
    * @param $categoryCode code of the statistical category.
32
    *
33
    * @return Statistic.
0 ignored issues
show
Documentation introduced by
The doc-type Statistic. could not be parsed: Unknown type name "Statistic." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
    */
35
    public function getStatistic($typeCode,$categoryCode)
36
    {
37
        $stats = array();
38
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
39
            if (($statistic->category_type->value == $typeCode) && 
40
                ($statistic->statistic_category->value == $categoryCode)) {
41
                array_push($stats,$statistic);
42
            }
43
        }
44
        return $stats;
45
    }
46
47
    /**
48
    * Add a user statistic with no regard to existing Types and Categories.
49
    *
50
    * @param string $typeCode code of the category type.
51
    * @param string $typeDesc description of the category type.
52
    * @param string $categoryCode code of the statistical category.
53
    * @param string $categoryDesc description of the statistical category.
54
    * @param string $note free text description of the statistic.
55
    * @param string $segment_type (Internal|External)
56
    *
57
    * @return Statistic
58
    */
59
    public function addStatisticRaw($typeCode,$typeDesc,$categoryCode,$categoryDesc,$segment_type,$note)
60
    {
61
        # Create the new statistic object
62
        $stat_obj = (object) [
63
            'statistic_category' => (object) [
64
                'value' => $categoryCode,
65
                'desc'  => $categoryDesc,
66
            ],
67
            'category_type' => (object) [
68
                'value' => $typeCode,
69
                'desc'  => $typeDesc,
70
            ],
71
            'statistic_note' => $note,
72
            'segment_type'   => $segment_type,
73
        ];
74
75
        # Add the object to the user
76
        $this->data[] = $stat_obj;
77
78
        /*  Not Sure this is quite right or needed. */
79
        #return Statistic::make($this->client,$stat_obj);
80
        #return($this->data);
81
    }
82
83
    /**
84
    * Add a user statistic based upon existing Types and Categories.
85
    *
86
    * @param string $typeCode code of the category type.
87
    * @param string $categoryCode code of the statistical category.
88
    *
89
    * @return Statistic
90
    */
91
    public function addStatistic($typeCode,$categoryCode,$segmentType,$note)
92
    {
93
        //* TODO HERE *//
94
95
        /* Logic: */
96
        /* Lookup both $typeCode and $categoryCode in codetable */
97
        /* Obtain information (code/description) from both code tables. */
98
        /* If found and ok, add the statistic to the user */
99
100
        /* Code Tables used:
101
            Code Table; UserStatisticalTypes
102
            Code Table: UserStatCategories
103
        */
104
105
        /* Seems to be an issue with # of categoryCodes that can be pulled at once (max:10) */
106
        /* But performing via curl returns all (???). */
107
        /* May need to open a case to allow resumption on pulling code tables regarding 'limit' and 'offset'? */
108
109
        #
110
        # These functions for CodeTables need defined.
111
        #
112
        #$typeDesc = $this->conf->CodeTables->getDesc('UserStatisticalTypes',$typeCode);
113
        #$categoryDesc = $this->conf->CodeTables->getDesc('UserStatCagegories',$categoryCode);
114
        #
115
        # These are temporary for testing.
116
        $typeDesc = 'Test';
117
        $categoryDesc = 'Test';
118
        
119
        $this->addStatisticRaw($typeCode,$typeDesc,$categoryCode,$categoryDesc,$segmentType,$note);
120
121
    }
122
123
    /**
124
    * Delete a user statistic.
125
    *
126
    * @param string $typeCode code of the category type.
127
    * @param string $categoryCode code of the statistical category.
128
    */
129
    public function removeStatistic($typeCode,$categoryCode)
130
    {
131
        $max = sizeof($this->data);
132
        $ret = false;
133
        for($i = 0; $i < $max; $i++) {
134
            if (($this->data[$i]->category_type->value == $typeCode) && ($this->data[$i]->statistic_category->value == $categoryCode)) {
135
                unset($this->data[$i]);
136
                $ret = true;
137
            }
138
        }
139
        return($ret);
140
    }
141
142
    /**
143
    * Update a user statistic.
144
    *
145
    */
146
    public function updateStatistic($fromTypeCode,$fromCategoryCode,$toTypeCode,$toCategoryCode,$segementType,$note)
147
    {
148
        /* Remove "from" statistic, then add "to" statistic */
149
        $this->removeStatistic($fromTypeCode,$categoryCode);
0 ignored issues
show
Bug introduced by
The variable $categoryCode does not exist. Did you mean $fromCategoryCode?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
150
        $this->addStatistic($toTypeCode,$toCategoryCode,$segmentType,$note);
0 ignored issues
show
Bug introduced by
The variable $segmentType does not exist. Did you mean $segementType?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
151
    }
152
153
    /**
154
    * Get Stats By Type Code.
155
    *
156
    * @return Array of Statistics.
157
    */
158 View Code Duplication
    public function getStatsByTypeCode($typeCode)
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...
159
    {
160
        $stats = array();
161
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
162
            if ($statistic->category_type->value == $typeCode) {
163
                array_push($stats,$statistic);
164
            }
165
    
166
        }
167
        return $stats;
168
    } 
169
170
    /**
171
    * Get Stats by Type Desc.
172
    *
173
    * @return Array of statistics.
174
    */
175 View Code Duplication
    public function getStatsByTypeDesc($typeDesc)
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...
176
    {
177
        $stats = array();
178
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
179
            if ($statistic->category_type->desc == $typeDesc) {
180
                array_push($stats,$statistic);
181
            }
182
        }
183
        return $stats;
184
    }
185
186
    /**
187
    * Get Stats by Category Code.
188
    *
189
    * @return Array of Statistics.
190
    */
191 View Code Duplication
    public function getStatsByCategoryCode($categoryCode)
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...
192
    {
193
        $stats = array();
194
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
195
            if ($statistic->statistic_category->value == $categoryCode) {
196
                array_push($stats,$statistic);
197
            }
198
        }
199
        return $stats;
200
    }
201
202
    /**
203
    * Get Stats by Category Desc.
204
    *
205
    * @return Array of Statistics.
206
    */
207 View Code Duplication
    public function getStatsByCategoryDesc($categoryDesc)
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...
208
    {
209
        $stats = array();
210
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
211
            if ($statistic->statistic_category->desc == $categoryDesc) {
212
                array_push($stats,$statistic);
213
            }
214
        }
215
        return $stats;
216
    }
217
218
    /**
219
    * Get Stats by Segment Type.
220
    *
221
    * @return Array of Statistics.
222
    */
223
    public function getStatsBySegmentType($segmentType)
224
    {
225
        $stats = array();
226
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
227
            if ($statistic->segment_type == $segmentType) {
228
                array_push($stats,$statistic);
229
            }
230
        }
231
        return $stats;
232
    }  
233
234
    /**
235
    * Search Stats by Note.
236
    *
237
    * @return Array of Statistics.
238
    */
239
    public function searchStatsByNote($note)
240
    {
241
        $stats = array();
242
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
243
            if (preg_match("/$note/i", $statistic->statistic_note)) {
244
                array_push($stats,$statistic);
245
            }
246
        }
247
        return $stats;
248
    }
249
250
    /** 
251
    * Search Stats by Type Code.
252
    *
253
    * @return Array of Statistics.
254
    */
255 View Code Duplication
    public function searchStatsByTypeCode($typeCode)
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...
256
    {
257
        $stats = array();
258
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
259
            if (preg_match("/$typeCode/i", $statistic->category_type->value)) {
260
                array_push($stats,$statistic);
261
            }
262
        }
263
        return $stats;
264
    }
265
266
    /**
267
    * Search Stats by Type Code.
268
    *
269
    * @return Array of Statistics.
270
    */
271 View Code Duplication
    public function searchStatsByTypeDesc($typeDesc)
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...
272
    {
273
        $stats = array();
274
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
275
            if (preg_match("/$typeDesc/i", $statistic->category_type->desc)) {
276
                array_push($stats,$statistic);
277
            }
278
        }
279
        return $stats;
280
    }
281
282
    /**
283
    * Search Stats by Type Code.
284
    *
285
    * @return Array of Statistics.
286
    */
287 View Code Duplication
    public function searchStatsByCategoryCode($categoryCode)
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...
288
    {
289
        $stats = array();
290
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
291
            if (preg_match("/$categoryCode/i", $statistic->statistic_category->value)) {
292
                array_push($stats,$statistic);
293
            }
294
        }
295
        return $stats;
296
    }
297
298
    /**
299
    * Search Stats by Type Code.
300
    *
301
    * @return Array of Statistics.
302
    */
303 View Code Duplication
    public function searchStatsByCategoryDesc($categoryDesc)
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...
304
    {
305
        $stats = array();
306
        foreach ($this->data as $statistic) {
0 ignored issues
show
Bug introduced by
The expression $this->data of type object<stdClass>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
307
            if (preg_match("/$categoryDesc/i", $statistic->statistic_category->desc)) {
308
                array_push($stats,$statistic);
309
            }
310
        }
311
        return $stats;
312
    }
313
314
}
315