Completed
Push — master ( 4c50f1...1ef0a2 )
by
unknown
02:50
created

AbstractSearch::modelFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Search;
4
5
use \RuntimeException;
6
7
use \Psr\Log\LoggerAwareInterface;
8
use \Psr\Log\LoggerAwareTrait;
9
10
/**
11
 *
12
 */
13
abstract class AbstractSearch implements SearchInterface, LoggerAwareInterface
14
{
15
    use LoggerAwareTrait;
16
17
    /**
18
     * The (raw) search results.
19
     * @var array $results
20
     */
21
    private $results;
22
23
    /**
24
     * [$modelFactory description]
25
     * @var [type]
26
     */
27
    protected $modelFactory;
28
29
30
    /**
31
     * @param array $data Class options and dependencies.
32
     */
33
    public function __construct(array $data)
34
    {
35
        $this->setLogger($data['logger']);
36
        $this->setModelFactory($data['modelFactory']);
37
    }
38
39
    /**
40
     * [setModelFactory description]
41
     * @param [type] $factory [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
42
     */
43
    public function setModelFactory($factory)
44
    {
45
        $this->modelFactory = $factory;
46
        return $this;
47
    }
48
49
    /**
50
     * [modelFactory description]
51
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
52
     */
53
    public function modelFactory()
54
    {
55
        return $this->modelFactory;
56
    }
57
58
    /**
59
     * @param string $keyword The searched-for keyword.
60
     * @return array
61
     */
62
    abstract public function search($keyword);
63
64
    /**
65
     * A search is always callable (alias to the "search" method).
66
     *
67
     * @param string $keyword The searched-for keyword.
68
     * @return array The results.
69
     */
70
    final public function __invoke($keyword)
71
    {
72
        return $this->search($keyword);
73
    }
74
75
    /**
76
     * @param string $resultType The type of results to search. Can be only "raw" for now.
77
     * @throws RuntimeException If this method is called before a search was executed.
78
     * @return array The results from the last search operation.
79
     */
80
    final public function lastResults($resultType = 'raw')
81
    {
82
        if ($this->results === null) {
83
            throw new RuntimeException(
84
                'Search was never performed'
85
            );
86
        }
87
88
        if ($resultType === 'raw') {
89
            return $this->lastResults;
0 ignored issues
show
Bug introduced by
The property lastResults does not seem to exist. Did you mean results?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
90
        }
91
    }
92
93
    /**
94
     * @param array $results The (raw) search results.
95
     * @return void
96
     */
97
    final protected function setResults(array $results)
98
    {
99
        $this->results = $results;
100
    }
101
}
102