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

MaxComplexityQueryVisitor::visit()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.2
ccs 8
cts 9
cp 0.8889
cc 4
eloc 9
nc 8
nop 3
crap 4.0218
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
}