Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Ordering / Cli / OrderController.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
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\Ordering\Cli;
12
13 use Behat\Testwork\Ordering\Exception\InvalidOrderException;
14 use Behat\Testwork\Ordering\OrderedExercise;
15 use Behat\Testwork\Ordering\Orderer\Orderer;
16 use Behat\Testwork\Cli\Controller;
17 use Symfony\Component\Console\Command\Command as SymfonyCommand;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
21 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
23 /**
24  * Preloads scenarios and then modifies the order when --order is passed
25  *
26  * @author Ciaran McNulty <mail@ciaranmcnulty.com>
27  */
28 final class OrderController implements Controller
29 {
30     /**
31      * @var EventDispatcherInterface
32      */
33     private $eventDispatcher;
34     /**
35      * @var OrderedExercise
36      */
37     private $exercise;
38     /**
39      * @var array
40      */
41     private $orderers = array();
42
43     /**
44      * Initializes controller.
45      *
46      * @param EventDispatcherInterface $eventDispatcher
47      * @param OrderedExercise $exercise
48      */
49     public function __construct(EventDispatcherInterface $eventDispatcher, OrderedExercise $exercise)
50     {
51         $this->eventDispatcher = $eventDispatcher;
52         $this->exercise = $exercise;
53     }
54
55     /**
56      * Configures command to be executable by the controller.
57      *
58      * @param SymfonyCommand $command
59      */
60     public function configure(SymfonyCommand $command)
61     {
62         $command->addOption('--order', null, InputOption::VALUE_REQUIRED,
63             'Set an order in which to execute the specifications (this will result in slower feedback).'
64         );
65     }
66
67     /**
68      * Executes controller.
69      *
70      * @param InputInterface $input
71      * @param OutputInterface $output
72      *
73      * @return null|integer
74      */
75     public function execute(InputInterface $input, OutputInterface $output)
76     {
77         $orderer = $input->getOption('order');
78
79         if (!$orderer) {
80             return;
81         }
82
83         if (!array_key_exists($orderer, $this->orderers)) {
84            throw new InvalidOrderException(sprintf("Order option '%s' was not recognised", $orderer));
85         }
86
87         $this->exercise->setOrderer($this->orderers[$orderer]);
88     }
89
90     /**
91      * Register a new available controller
92      *
93      * @param Orderer $orderer
94      */
95     public function registerOrderer(Orderer $orderer)
96     {
97         $this->orderers[$orderer->getName()] = $orderer;
98     }
99 }