Completed
Pull Request — master (#15)
by
unknown
03:53
created

MaxComplexityQueryVisitor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 33
rs 10
ccs 12
cts 13
cp 0.9231

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A visit() 0 12 4
1
<?php
2
3
namespace Youshido\GraphQL\Execution\Visitor;
4
5
6
use Youshido\GraphQL\Config\Field\FieldConfig;
7
8
class MaxComplexityQueryVisitor extends AbstractQueryVisitor {
9
10
  public $maxScore;
11
12
  protected $defaultScore = 1;
13
14 1
  public function __construct($max) {
15 1
    parent::__construct();
16
17 1
    $this->maxScore = $max;
18 1
  }
19
20
  /**
21
   * @param array       $args
22
   * @param FieldConfig $fieldConfig
23
   * @param int         $childScore
24
   *
25
   * @return mixed
26
   * @throws \Exception
27
   */
28 1
  public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) {
29 1
    $cost = $fieldConfig->get('cost');
30 1
    if (is_callable($cost)) {
31
      $cost = $cost($args, $fieldConfig, $childScore);
32
    }
33 1
    $cost = $cost ?: $this->defaultScore;
34 1
    $this->memo += $cost;
35 1
    if ($this->memo > $this->maxScore) {
36 1
      throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore);
37
    }
38 1
    return $cost;
39
  }
40
}