Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Hook / HookRepository.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Hook;
12
13 use Behat\Testwork\Call\Callee;
14 use Behat\Testwork\Environment\Environment;
15 use Behat\Testwork\Environment\EnvironmentManager;
16 use Behat\Testwork\Hook\Scope\HookScope;
17
18 /**
19  * Finds hooks using provided environments or scopes.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 final class HookRepository
24 {
25     /**
26      * @var EnvironmentManager
27      */
28     private $environmentManager;
29
30     /**
31      * Initializes repository.
32      *
33      * @param EnvironmentManager $environmentManager
34      */
35     public function __construct(EnvironmentManager $environmentManager)
36     {
37         $this->environmentManager = $environmentManager;
38     }
39
40     /**
41      * Returns all available hooks for a specific environment.
42      *
43      * @param Environment $environment
44      *
45      * @return Hook[]
46      */
47     public function getEnvironmentHooks(Environment $environment)
48     {
49         return array_filter(
50             $this->environmentManager->readEnvironmentCallees($environment),
51             function (Callee $callee) {
52                 return $callee instanceof Hook;
53             }
54         );
55     }
56
57     /**
58      * Returns hooks for a specific event.
59      *
60      * @param HookScope $scope
61      *
62      * @return Hook[]
63      */
64     public function getScopeHooks(HookScope $scope)
65     {
66         return array_filter(
67             $this->getEnvironmentHooks($scope->getEnvironment()),
68             function (Hook $hook) use ($scope) {
69                 if ($scope->getName() !== $hook->getScopeName()) {
70                     return false;
71                 }
72
73                 return !($hook instanceof FilterableHook && !$hook->filterMatches($scope));
74             }
75         );
76     }
77 }