| Total Complexity | 64 |
| Total Lines | 586 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like StatementGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StatementGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class StatementGenerator { |
||
| 9 | |||
| 10 | const LL_COURSE_EXTENSION = 'http://learninglocker.net/extensions/course'; |
||
| 11 | const COMMENT_EXTENSION = 'http://curatr3.com/define/extension/commentText'; |
||
| 12 | const BADGE_EXTENSION = 'http://specification.openbadges.org/xapi/extensions/badgeassertion'; |
||
| 13 | const MCQ_SINGLE_EXTENSION = 'http://curatr3.com/define/extension/single-mcq-option'; |
||
| 14 | const MCQ_MULTIPLE_EXTENSION = 'http://curatr3.com/define/extension/multiple-mcq-options'; |
||
| 15 | const CONTRIBUTION_OBJECT = 'http://curatr3.com/define/extension/contribution-object'; |
||
| 16 | const BADGE_TYPE = 'http://activitystrea.ms/schema/1.0/badge'; |
||
| 17 | const CATEGORY_TYPE = 'http://activitystrea.ms/schema/1.0/collection'; |
||
| 18 | const COLLECTION_TYPE = 'http://curatr3.com/define/type/collection'; |
||
| 19 | const COMMENT_TYPE = 'http://activitystrea.ms/schema/1.0/comment'; |
||
| 20 | const COURSE_TYPE = 'http://adlnet.gov/expapi/activities/course'; |
||
| 21 | const FILE_TYPE = 'http://activitystrea.ms/schema/1.0/file'; |
||
| 22 | const GATE_TYPE = 'http://curatr3.com/define/type/gate'; |
||
| 23 | const LEVEL_TYPE = 'http://curatr3.com/define/type/level'; |
||
| 24 | const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/article'; |
||
| 25 | const ORGANISATION_TYPE = 'http://curatr3.com/define/type/organisation'; |
||
| 26 | const PAGE_TYPE = 'http://activitystrea.ms/schema/1.0/page'; |
||
| 27 | const QUESTION_TYPE = 'http://activitystrea.ms/schema/1.0/question'; |
||
| 28 | const RESPONSE_TYPE = 'http://curatr3.com/define/type/response'; |
||
| 29 | const SECTION_TYPE = 'http://activitystrea.ms/schema/1.0/collection'; |
||
| 30 | const TAG_TYPE = 'http://id.tincanapi.com/activitytype/tag'; |
||
| 31 | const TAG_TYPE_EXTENSION = 'http://curatr3.com/define/extension/tag-type'; |
||
| 32 | |||
| 33 | public $url, $statement, $endpoints, $log; |
||
| 34 | |||
| 35 | public function __construct( $endpoints=array() ) { |
||
| 36 | |||
| 37 | $this->log = \App::make('xapi.log'); |
||
| 38 | $this->endpoints = $endpoints; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Checks a passed array has a defined key, returns a default if not set |
||
| 43 | * @param array $array The array to check |
||
| 44 | * @param string $key Key to check |
||
| 45 | * @param type $default Default to return if key is not set |
||
|
|
|||
| 46 | * @return mixed |
||
| 47 | */ |
||
| 48 | private static function checkKey( $array, $key, $default='' ){ |
||
| 49 | return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates a statement from an array, with defaults if needed |
||
| 54 | * @param Array $data Holds the actor, verb and object information |
||
| 55 | */ |
||
| 56 | public function buildStatement($data) { |
||
| 57 | |||
| 58 | $actor = $this->checkKey($data, 'actor'); |
||
| 59 | $verb = $this->checkKey($data, 'verb', self::getVerb('experienced') ); |
||
| 60 | $object = $this->checkKey($data, 'object'); |
||
| 61 | $authority = $this->checkKey($data, 'authority', []); |
||
| 62 | $context = $this->checkKey($data, 'context', [] ); |
||
| 63 | $result = $this->checkKey($data, 'result', [] ); |
||
| 64 | $timestamp = $this->checkKey($data, 'timestamp', Carbon::now()->format("c") ); |
||
| 65 | |||
| 66 | $this->statement = array( |
||
| 67 | 'actor' => $actor, |
||
| 68 | 'verb' => $verb, |
||
| 69 | 'object' => $object, |
||
| 70 | 'timestamp' => $timestamp |
||
| 71 | ); |
||
| 72 | |||
| 73 | if( isset($result) && !empty($result) ){ |
||
| 74 | $this->statement['result'] = $result; |
||
| 75 | } |
||
| 76 | |||
| 77 | if( isset($authority) && !empty($authority) ){ |
||
| 78 | $this->statement['authority'] = $authority; |
||
| 79 | } |
||
| 80 | |||
| 81 | if( isset($context) && !empty($context) ){ |
||
| 82 | $this->statement['context'] = $context; |
||
| 83 | } else { |
||
| 84 | $this->statement['context'] = $this->createContext(); |
||
| 85 | } |
||
| 86 | |||
| 87 | //Add the org to the contextActivites |
||
| 88 | $org = \Organisation::userCurrent(); |
||
| 89 | if( $org ){ |
||
| 90 | if( !isset($this->statement['context']['contextActivities']) ){ |
||
| 91 | $this->statement['context']['contextActivities'] = []; |
||
| 92 | } |
||
| 93 | |||
| 94 | if( !isset($this->statement['context']['contextActivities']['grouping']) ){ |
||
| 95 | $this->statement['context']['contextActivities']['grouping'] = []; |
||
| 96 | } elseif( !isset($this->statement['context']['contextActivities']['grouping'][0])){ |
||
| 97 | $val = $this->statement['context']['contextActivities']['grouping']; |
||
| 98 | $this->statement['context']['contextActivities']['grouping'] = [ $val ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->statement['context']['contextActivities']['grouping'][] = $org->tcExtension(); |
||
| 102 | } |
||
| 103 | |||
| 104 | // remove context.platform if NOT an activity (xAPI 4.1.6) |
||
| 105 | if ( |
||
| 106 | isset($this->statement['context']) |
||
| 107 | && isset($this->statement['object'], $this->statement['object']['objectType']) |
||
| 108 | && strtolower($this->statement['object']['objectType']) !== strtolower('activity') |
||
| 109 | ) { |
||
| 110 | if (isset($this->statement['context']['platform'])) { |
||
| 111 | unset($this->statement['context']['platform']); |
||
| 112 | } |
||
| 113 | if (isset($this->statement['context']['revision'])) { |
||
| 114 | unset($this->statement['context']['revision']); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | return $this->statement; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Static method |
||
| 124 | * Generates the actor part of the statement |
||
| 125 | * @param string $name |
||
| 126 | * @param string $email |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public static function createMboxActor( $name, $email ){ |
||
| 130 | return array( |
||
| 131 | 'name' => $name, |
||
| 132 | 'mbox' => 'mailto:' . $email, |
||
| 133 | 'objectType' => 'Agent' |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Static method |
||
| 139 | * Generates the actor part of the statement |
||
| 140 | * @param string $name |
||
| 141 | * @param string $homepage |
||
| 142 | * @param string $ref |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public static function createAccountActor( $name, $homepage, $ref ){ |
||
| 146 | return [ |
||
| 147 | 'name' => $name, |
||
| 148 | 'account' => ['homePage' => $homepage, 'name' => $ref], |
||
| 149 | 'objectType' => 'Agent' |
||
| 150 | ]; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Static method |
||
| 155 | * Generates the authority part of the statement |
||
| 156 | * @param string $name |
||
| 157 | * @param string $email |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public static function createAuthority( $name, $email ){ |
||
| 161 | return array( |
||
| 162 | 'name' => $name, |
||
| 163 | 'mbox' => 'mailto:' . $email, |
||
| 164 | 'objectType' => 'Agent' |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Static method |
||
| 170 | * Generates the result part of the statement |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public static function createResult( $result=array() ){ |
||
| 174 | |||
| 175 | return $result; |
||
| 176 | |||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Static method |
||
| 181 | * Generates the context extension part of the statement |
||
| 182 | * @todo hardcoded for now |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public static function createContext( $extensions = array(), $additional=null ){ |
||
| 186 | |||
| 187 | $context = array( |
||
| 188 | 'platform' => 'Curatr' |
||
| 189 | ); |
||
| 190 | |||
| 191 | if( !empty($extensions) ){ |
||
| 192 | $context['extensions'] = $extensions; |
||
| 193 | } |
||
| 194 | |||
| 195 | if( !is_null($additional) ){ |
||
| 196 | $context = array_merge($context, $additional ); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $context; |
||
| 200 | |||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Static method |
||
| 205 | * Returns a predefined verb or 'experienced' if not found |
||
| 206 | * @param string $key The verb to use |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public static function getVerb( $key ){ |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Create the object section of statement |
||
| 326 | * |
||
| 327 | * @param int $id |
||
| 328 | * @param string $name |
||
| 329 | * @param string $description |
||
| 330 | * @param array $extensions |
||
| 331 | * @return array |
||
| 332 | * |
||
| 333 | */ |
||
| 334 | public static function createObject( $id, $name=null, $description=null, $type=null, $extensions = array() ) |
||
| 335 | { |
||
| 336 | $object = array( |
||
| 337 | 'objectType' => 'Activity', |
||
| 338 | 'id' => $id |
||
| 339 | ); |
||
| 340 | |||
| 341 | $definition = array(); |
||
| 342 | |||
| 343 | if (!empty($name)) { |
||
| 344 | $definition['name'] = [ |
||
| 345 | 'en-GB' => $name |
||
| 346 | ]; |
||
| 347 | } |
||
| 348 | |||
| 349 | if( !empty($description)){ |
||
| 350 | $definition['description'] = [ |
||
| 351 | 'en-GB'=>$description |
||
| 352 | ]; |
||
| 353 | } |
||
| 354 | |||
| 355 | if( !empty($type)){ |
||
| 356 | $definition['type'] = $type; |
||
| 357 | } |
||
| 358 | |||
| 359 | if( !empty($extensions) ){ |
||
| 360 | $definition['extensions'] = $extensions; |
||
| 361 | } |
||
| 362 | |||
| 363 | if (!empty($definition)) { |
||
| 364 | $object['definition'] = $definition; |
||
| 365 | } |
||
| 366 | |||
| 367 | return $object; |
||
| 368 | } |
||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | public function sendRequests() { |
||
| 373 | foreach( $this->endpoints as $name => $ep ){ |
||
| 374 | |||
| 375 | $username = $ep->username; |
||
| 376 | $password = $ep->key; |
||
| 377 | $url = $ep->url; |
||
| 378 | |||
| 379 | if(substr($url, -1) !== '/') { |
||
| 380 | $url .= '/'; |
||
| 381 | } |
||
| 382 | |||
| 383 | \Queue::push('\HT2\Queues\XAPIService', [ |
||
| 384 | 'endpoint' => $url, |
||
| 385 | 'type' => 'basic', |
||
| 386 | 'username' => $username, |
||
| 387 | 'password' => $password, |
||
| 388 | 'statement' => $this->statement |
||
| 389 | ]); |
||
| 390 | |||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Send the request to all end points defined in the endpoints array |
||
| 396 | * @return array An array of all requests sent and their responses |
||
| 397 | */ |
||
| 398 | public function sendRequestsOld(){ |
||
| 399 | $error = $this->checkError(); |
||
| 400 | if( $error ) return false; |
||
| 401 | |||
| 402 | $results = array(); |
||
| 403 | |||
| 404 | |||
| 405 | foreach( $this->endpoints as $name => $ep ){ |
||
| 406 | |||
| 407 | $username = $ep->username; |
||
| 408 | $password = $ep->key; |
||
| 409 | $url = $ep->url; |
||
| 410 | $auth = base64_encode( $username.':'.$password ); |
||
| 411 | |||
| 412 | $result = $this->make_request($auth, $url, $this->statement); |
||
| 413 | $error = false; |
||
| 414 | $error_type = ""; |
||
| 415 | $body = ""; |
||
| 416 | |||
| 417 | if( isset($result['error']) ){ |
||
| 418 | $error = true; |
||
| 419 | $error_type = "cURL error"; |
||
| 420 | } else { |
||
| 421 | |||
| 422 | $body = isset($result['body']) ? json_decode($result['body']) : ""; |
||
| 423 | |||
| 424 | if( isset($body->success) ){ |
||
| 425 | if( $body->success == false ){ |
||
| 426 | $error = true; |
||
| 427 | $error_type = "LRS returned success as false"; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | if( isset($body->error) ){ |
||
| 432 | if( $body->error == true ){ |
||
| 433 | $error = true; |
||
| 434 | $error_type = "LRS returned error as true"; |
||
| 435 | } |
||
| 436 | } |
||
| 437 | if( isset($result['status']) ){ |
||
| 438 | $status = $result['status']; |
||
| 439 | |||
| 440 | switch( intval($status) ){ |
||
| 441 | case 400: |
||
| 442 | case 404: |
||
| 443 | case 500: |
||
| 444 | $error = true; |
||
| 445 | $error_type = "LRS returned HTTP code ".$status; |
||
| 446 | break; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | if( $error ){ |
||
| 452 | $this->log->error( 'Statement Failed', array( |
||
| 453 | 'curl_result' => $result, |
||
| 454 | 'type' => $error_type, |
||
| 455 | 'url' => $ep->url, |
||
| 456 | 'statement' => $this->statement |
||
| 457 | )); |
||
| 458 | } else { |
||
| 459 | $this->log->info( 'Sent', array( |
||
| 460 | 'url' => $ep->url, |
||
| 461 | 'statement' => $this->statement |
||
| 462 | )); |
||
| 463 | } |
||
| 464 | |||
| 465 | $results[$url] = $body; |
||
| 466 | } |
||
| 467 | |||
| 468 | |||
| 469 | return array( |
||
| 470 | 'statement' => $this->statement, |
||
| 471 | 'endpoints' => $this->endpoints, |
||
| 472 | 'results' => $results |
||
| 473 | ); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Send statement |
||
| 478 | * @param string $auth The base64 basic auth info for the request |
||
| 479 | * @param string $url The endpoint |
||
| 480 | * @param array $statement The statement to be encoded |
||
| 481 | * @return boolean/array Returns false on fail, array of details on success |
||
| 482 | */ |
||
| 483 | public function make_request( $auth, $url, $statement ) { |
||
| 484 | |||
| 485 | $headers = array( |
||
| 486 | 'Authorization: Basic '.$auth, |
||
| 487 | 'Content-Type: application/json; charset=UTF-8', |
||
| 488 | 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
||
| 489 | 'X-Experience-API-Version: 1.0.0' |
||
| 490 | ); |
||
| 491 | |||
| 492 | if(substr($url, -1) !== '/') { |
||
| 493 | $url .= '/'; |
||
| 494 | } |
||
| 495 | |||
| 496 | $ch = curl_init( $url . 'statements' ); |
||
| 497 | curl_setopt( $ch, CURLOPT_POST, 1); |
||
| 498 | curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($statement) ); |
||
| 499 | curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); |
||
| 500 | curl_setopt( $ch, CURLOPT_HEADER, 1); |
||
| 501 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 502 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers ); |
||
| 503 | |||
| 504 | if( substr($url, 0, 5) == "https" ){ |
||
| 505 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
| 506 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
||
| 507 | curl_setopt ($ch, CURLOPT_CAINFO, storage_path()."/cacert.pem"); |
||
| 508 | } |
||
| 509 | |||
| 510 | $response = curl_exec( $ch ); |
||
| 511 | if( $response === false ){ |
||
| 512 | return array('error'=>true, 'curl_error'=>curl_error($ch)); |
||
| 513 | } else { |
||
| 514 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||
| 515 | $header = substr($response, 0, $header_size); |
||
| 516 | $body = substr($response, $header_size); |
||
| 517 | return array( |
||
| 518 | 'ch' => $ch, |
||
| 519 | 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE), |
||
| 520 | 'header' => $header, |
||
| 521 | 'body' => $body |
||
| 522 | ); |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | private function checkError(){ |
||
| 548 | } |
||
| 549 | |||
| 550 | private function error( $message ){ |
||
| 551 | $this->log->error( "Statement Generator Error: ".$message ); |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * We will probably move this to the db, or hook into an existing service? |
||
| 556 | * |
||
| 557 | **/ |
||
| 558 | private static function returnAvailableVerbs(){ |
||
| 559 | return array( |
||
| 560 | 'answered', |
||
| 561 | 'asked', |
||
| 562 | 'attempted', |
||
| 563 | 'attended', |
||
| 564 | 'commented', |
||
| 565 | 'completed', |
||
| 566 | 'exited', |
||
| 567 | 'experienced', |
||
| 568 | 'failed', |
||
| 569 | 'imported', |
||
| 570 | 'initialized', |
||
| 571 | 'interacted', |
||
| 572 | 'launched', |
||
| 573 | 'mastered', |
||
| 574 | 'passed', |
||
| 575 | 'preferred', |
||
| 576 | 'progressed', |
||
| 577 | 'registered', |
||
| 578 | 'responded', |
||
| 579 | 'resumed', |
||
| 580 | 'scored', |
||
| 581 | 'shared', |
||
| 582 | 'suspended', |
||
| 583 | 'terminated', |
||
| 584 | 'voided'); |
||
| 585 | } |
||
| 586 | |||
| 587 | public static function makeEndpoint( $url, $username, $password ){ |
||
| 594 | } |
||
| 595 | } |
||
| 596 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths