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

MaxComplexityQueryVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Youshido\GraphQL\Execution\Visitor;
4
5
6
use Youshido\GraphQL\Field\AbstractField;
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 1
  public function visit(AbstractField $field) {
21 1
    $cost = $field->getConfig()->get('cost');
22 1
    if (is_callable($cost)) {
23
      $cost = $cost();
24
    }
25 1
    $this->memo += $cost ?: $this->defaultScore;
26 1
    if ($this->memo > $this->maxScore) {
27 1
      throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore);
28
    }
29 1
    return $this->memo;
30
  }
31
}