Query::statementForward()   D
last analyzed

Complexity

Conditions 14
Paths 261

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 43
rs 4.6708
cc 14
nc 261
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace LearningLocker\StatementForwarding;
3
4
use Curatr\Object\Completion\Types;
0 ignored issues
show
Bug introduced by
The type Curatr\Object\Completion\Types was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Curatr\ObjectCompletionCriterion;
0 ignored issues
show
Bug introduced by
The type Curatr\ObjectCompletionCriterion was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use GuzzleHttp\Exception\RequestException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Exception\RequestException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use GuzzleHttp\Exception\ClientException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Exception\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class Query {
10
11
  const COMMENT = "AUTOGENERATED BY - DO NOT MANUALLY AMEND";
12
13
  /**
14
   * Generate the Statement Forward Query
15
   * based on the Object Completion Criteria
16
   */
17
  public static function statementForward($org_id) {
18
    $criteria = ObjectCompletionCriterion::where('org_id', $org_id)->whereHas(\Object::class, function($q){
19
      $q->where("completion_type", Types::XAPI);
20
    })->with('object')->get();
21
22
    if (sizeof($criteria) === 0 ){
23
      // if there are no criteria
24
      return false;
25
    } else {
26
      //if there is criteria
27
      $queries = [];
28
29
      foreach ($criteria as $criterion) {
30
        $course = $criterion->object->course;
31
        if (empty($course) || ($course && !empty($course->deleted_at))) {
32
          continue;
33
        }
34
        $criteriaQuery = [];
35
        $criteriaQuery['$comment'] = 'id:'  . $criterion->id . ', object_id:' . $criterion->object_id;
36
        $activity_id = $criterion->use_curatr_activity ? $criterion->object->getXapiUrl(true, true) : $criterion->activity_id;
37
        $criteriaQuery['statement.object.id'] = $activity_id;
38
        if (!empty($criterion->verb)) $criteriaQuery['statement.verb.id'] = $criterion->verb;
39
        if (!empty($criterion->completion)) $criteriaQuery['statement.result.completion'] = true;
40
        if (!empty($criterion->success)) $criteriaQuery['statement.result.success'] = true;
41
        if (is_numeric($criterion->raw)) $criteriaQuery['statement.result.score.raw'] = ['$gte' => floatval($criterion->raw)];
42
        if (is_numeric($criterion->scaled)) $criteriaQuery['statement.result.score.scaled'] = ['$gte' => floatval($criterion->scaled)];
43
44
        if (sizeof($criteriaQuery) > 0) {
45
          $queries[] = $criteriaQuery;
46
        }
47
      }
48
      if (sizeof($queries) === 0 ){
49
        // if there are no quries
50
        return false;
51
      }
52
53
      $query = [
54
        '$comment' => self::COMMENT,
55
        '$or'=>$queries
56
      ];
57
    }
58
59
    return json_encode($query);
60
  }
61
62
  /**
63
   * Get the Learning Locker Statement Forward by ID.
64
   *
65
   * @param   $id
66
   * @return  $response
0 ignored issues
show
Documentation Bug introduced by
The doc comment $response at position 0 could not be parsed: Unknown type name '$response' at position 0 in $response.
Loading history...
67
   */
68
  protected function get() {
69
    try {
70
      $url = $this->endpoint . $this->api . $this->v2 . $this->query . '/';
0 ignored issues
show
Bug Best Practice introduced by
The property v2 does not exist on LearningLocker\StatementForwarding\Query. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property endpoint does not exist on LearningLocker\StatementForwarding\Query. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property query does not exist on LearningLocker\StatementForwarding\Query. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property api does not exist on LearningLocker\StatementForwarding\Query. Did you maybe forget to declare it?
Loading history...
71
      $request = $this->getClient()->get($url, [
0 ignored issues
show
Bug introduced by
The method getClient() does not exist on LearningLocker\StatementForwarding\Query. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
      $request = $this->/** @scrutinizer ignore-call */ getClient()->get($url, [

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...
72
        'auth' => $this->getAuth(),
0 ignored issues
show
Bug introduced by
The method getAuth() does not exist on LearningLocker\StatementForwarding\Query. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        'auth' => $this->/** @scrutinizer ignore-call */ getAuth(),

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...
73
        'headers' => [
74
          'content-type' => 'application/json'
75
        ],
76
      ]);
77
78
      if($request->getStatusCode() === 404) {
79
        throw new ClientException('There was a issue connecting to Learning Locker.');
80
      }
81
      $response = $request->json();
82
    } catch (ClientException $e) {
83
      return $e;
84
    }
85
86
    return $response;
87
  }
88
89
}
90