4 * This file is part of the Behat Testwork.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Hook;
13 use Behat\Testwork\Call\CallCenter;
14 use Behat\Testwork\Call\CallResult;
15 use Behat\Testwork\Call\CallResults;
16 use Behat\Testwork\Hook\Call\HookCall;
17 use Behat\Testwork\Hook\Scope\HookScope;
20 * Dispatches registered hooks for provided events.
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 final class HookDispatcher
36 * Initializes hook dispatcher.
38 * @param HookRepository $repository
39 * @param CallCenter $callCenter
41 public function __construct(HookRepository $repository, CallCenter $callCenter)
43 $this->repository = $repository;
44 $this->callCenter = $callCenter;
48 * Dispatches hooks for a specified event.
50 * @param HookScope $scope
54 public function dispatchScopeHooks(HookScope $scope)
57 foreach ($this->repository->getScopeHooks($scope) as $hook) {
58 $results[] = $this->dispatchHook($scope, $hook);
61 return new CallResults($results);
65 * Dispatches single event hook.
67 * @param HookScope $scope
72 private function dispatchHook(HookScope $scope, Hook $hook)
74 return $this->callCenter->makeCall(new HookCall($scope, $hook));