Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TreenodeFactory
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isInitialized
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 initialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTreenodeCollectionFactory
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 makeNode
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6declare(strict_types=1);
7
8namespace pvc\struct\tree\node;
9
10use pvc\interfaces\struct\collection\CollectionFactoryInterface;
11use pvc\interfaces\struct\tree\node\TreenodeFactoryInterface;
12use pvc\interfaces\struct\tree\node\TreenodeInterface;
13use pvc\interfaces\struct\tree\tree\TreeInterface;
14use pvc\struct\tree\err\ChildCollectionException;
15use pvc\struct\tree\err\TreenodeFactoryNotInitializedException;
16use pvc\struct\tree\tree\Tree;
17
18/**
19 * Class TreenodeFactory
20 *
21 * Tree and TreenodeFactory are mutually dependent.  The constructors are set up so that you create
22 * TreenodeFactory first without its tree dependency, use TreenodeFactory in the construction of a new tree,
23 * and then go back and set the tree property in TreenodeFactory.  Tree factory does all this in the method
24 * makeTree.
25 */
26class TreenodeFactory implements TreenodeFactoryInterface
27{
28    /**
29     * @var TreeInterface
30     */
31    protected TreeInterface $tree;
32
33    /**
34     * @param CollectionFactoryInterface<TreenodeInterface> $treenodeCollectionFactory
35     */
36    public function __construct(
37        protected CollectionFactoryInterface $treenodeCollectionFactory,
38    ) {
39    }
40
41    public function isInitialized(): bool
42    {
43        return isset($this->tree);
44    }
45
46    /**
47     * @param Tree $tree
48     * @return void
49     */
50    public function initialize(TreeInterface $tree): void
51    {
52        $this->tree = $tree;
53    }
54
55    /**
56     * @return CollectionFactoryInterface<TreenodeInterface>
57     */
58    public function getTreenodeCollectionFactory(): CollectionFactoryInterface
59    {
60        if (!$this->isInitialized()) {
61            throw new TreeNodeFactoryNotInitializedException();
62        }
63        return $this->treenodeCollectionFactory;
64    }
65
66    /**
67     * @return Treenode
68     * @throws ChildCollectionException|TreenodeFactoryNotInitializedException
69     */
70    public function makeNode(): TreenodeInterface
71    {
72        if (!$this->isInitialized()) {
73            throw new TreeNodeFactoryNotInitializedException();
74        }
75        $treenodeCollection = $this->treenodeCollectionFactory->makeCollection([]);
76        return new Treenode($treenodeCollection, $this->tree);
77    }
78}