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

MaxComplexityQueryVisitor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 24
rs 10
ccs 11
cts 13
cp 0.8462

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A visit() 0 11 4
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
}