ConjunctionInterceptor.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace TYPO3\PharStreamWrapper\Interceptor;
  3. /*
  4. * This file is part of the TYPO3 project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under the terms
  7. * of the MIT License (MIT). For the full copyright and license information,
  8. * please read the LICENSE file that was distributed with this source code.
  9. *
  10. * The TYPO3 project - inspiring people to share!
  11. */
  12. use TYPO3\PharStreamWrapper\Assertable;
  13. use TYPO3\PharStreamWrapper\Exception;
  14. class ConjunctionInterceptor implements Assertable
  15. {
  16. /**
  17. * @var Assertable[]
  18. */
  19. private $assertions;
  20. public function __construct(array $assertions)
  21. {
  22. $this->assertAssertions($assertions);
  23. $this->assertions = $assertions;
  24. }
  25. /**
  26. * Executes assertions based on all contained assertions.
  27. *
  28. * @param string $path
  29. * @param string $command
  30. * @return bool
  31. * @throws Exception
  32. */
  33. public function assert($path, $command)
  34. {
  35. if ($this->invokeAssertions($path, $command)) {
  36. return true;
  37. }
  38. throw new Exception(
  39. sprintf(
  40. 'Assertion failed in "%s"',
  41. $path
  42. ),
  43. 1539625084
  44. );
  45. }
  46. /**
  47. * @param Assertable[] $assertions
  48. */
  49. private function assertAssertions(array $assertions)
  50. {
  51. foreach ($assertions as $assertion) {
  52. if (!$assertion instanceof Assertable) {
  53. throw new \InvalidArgumentException(
  54. sprintf(
  55. 'Instance %s must implement Assertable',
  56. get_class($assertion)
  57. ),
  58. 1539624719
  59. );
  60. }
  61. }
  62. }
  63. /**
  64. * @param string $path
  65. * @param string $command
  66. * @return bool
  67. */
  68. private function invokeAssertions($path, $command)
  69. {
  70. try {
  71. foreach ($this->assertions as $assertion) {
  72. if (!$assertion->assert($path, $command)) {
  73. return false;
  74. }
  75. }
  76. } catch (Exception $exception) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. }