Group   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeContentType() 0 3 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A getIterator() 0 3 1
A count() 0 3 1
A __construct() 0 5 2
B createSerializer() 0 15 6
A offsetSet() 0 9 3
A offsetUnset() 0 6 2
1
<?php
2
3
namespace FluentDOM\Serializer\Factory {
4
5
  use FluentDOM\Exceptions;
6
  use FluentDOM\Serializer\Factory as SerializerFactory;
7
8
  class Group implements SerializerFactory, \ArrayAccess, \IteratorAggregate, \Countable {
9
10
    private $_factories = [];
11
12 9
    public function __construct(array $factories = []) {
13 9
      foreach ($factories as $contentType => $factory) {
14 6
        $this->offsetSet($contentType, $factory);
15
      }
16 9
    }
17
18 3
    public function createSerializer(string $contentType, \DOMNode $node) {
19 3
      $serializer = NULL;
20 3
      if ($this->offsetExists($contentType)) {
21 3
        $factory = $this->offsetGet($contentType);
22 3
        if ($factory instanceof SerializerFactory) {
23 2
          $serializer = $factory->createSerializer($contentType, $node);
24 1
        } elseif (is_callable($factory)) {
25 1
          $serializer = $factory($contentType, $node);
26
        }
27 3
        if (NULL !== $serializer && !method_exists($serializer, '__toString')) {
28 1
          throw new Exceptions\InvalidSerializer($contentType, get_class($serializer));
29
        }
30
      }
31 2
      return $serializer;
32
    }
33
34 8
    private function normalizeContentType($contentType) {
35 8
      return strtolower($contentType);
36
    }
37
38 4
    public function offsetExists($contentType) {
39 4
      $contentType = $this->normalizeContentType($contentType);
40 4
      return array_key_exists($contentType, $this->_factories);
41
    }
42
43 8
    public function offsetSet($contentType, $factory) {
44 8
      $contentType = $this->normalizeContentType($contentType);
45 8
      if (!($factory instanceOf SerializerFactory || is_callable($factory))) {
46 1
        throw new Exceptions\InvalidArgument(
47 1
          'factory', 'FluentDOM\Serializer\Factory, callable'
48
        );
49
      }
50 7
      $this->_factories[$contentType] = $factory;
51 7
    }
52
53 5
    public function offsetGet($contentType) {
54 5
      $contentType = $this->normalizeContentType($contentType);
55 5
      return $this->_factories[$contentType];
56
    }
57
58 1
    public function offsetUnset($contentType) {
59 1
      $contentType = $this->normalizeContentType($contentType);
60 1
      if (array_key_exists($contentType, $this->_factories)) {
61 1
        unset($this->_factories[$contentType]);
62
      }
63 1
    }
64
65 1
    public function getIterator() {
66 1
      return new \ArrayIterator($this->_factories);
67
    }
68
69 2
    public function count() {
70 2
      return count($this->_factories);
71
    }
72
  }
73
}
74