|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of the CleverAge/ProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2019 Clever-Age |
| 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 CleverAge\ProcessBundle\Task\File; |
| 12 | + |
| 13 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 14 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 15 | +use Symfony\Component\Filesystem\Exception\IOException; |
| 16 | +use Symfony\Component\OptionsResolver\Exception\AccessException; |
| 17 | +use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; |
| 18 | +use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; |
| 19 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 20 | + |
| 21 | +/** |
| 22 | + * Read the whole file and output its content |
| 23 | + * |
| 24 | + * @todo Provide additional safeguards like if file exists and is readable |
| 25 | + * |
| 26 | + * @author Vincent Chalnot <[email protected]> |
| 27 | + */ |
| 28 | +class FileReaderTask extends AbstractConfigurableTask |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @param ProcessState $state |
| 32 | + * |
| 33 | + * @throws ExceptionInterface |
| 34 | + * @throws IOException |
| 35 | + */ |
| 36 | + public function execute(ProcessState $state) |
| 37 | + { |
| 38 | + $options = $this->getOptions($state); |
| 39 | + |
| 40 | + $state->setOutput(file_get_contents($options['filename'])); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param OptionsResolver $resolver |
| 45 | + * |
| 46 | + * @throws AccessException |
| 47 | + * @throws UndefinedOptionsException |
| 48 | + */ |
| 49 | + protected function configureOptions(OptionsResolver $resolver) |
| 50 | + { |
| 51 | + $resolver->setRequired( |
| 52 | + [ |
| 53 | + 'filename', |
| 54 | + ] |
| 55 | + ); |
| 56 | + $resolver->setAllowedTypes('filename', ['string']); |
| 57 | + } |
| 58 | +} |
0 commit comments