Completed
Push — 1.5 ( 17c098...006ba9 )
by Kamil
30:59
created

NotificationChecker::resolveClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Service;
15
16
use Sylius\Behat\Exception\NotificationExpectationMismatchException;
17
use Sylius\Behat\NotificationType;
18
use Sylius\Behat\Service\Accessor\NotificationAccessorInterface;
19
use Webmozart\Assert\Assert;
20
21
final class NotificationChecker implements NotificationCheckerInterface
22
{
23
    /** @var NotificationAccessorInterface */
24
    private $notificationAccessor;
25
26
    public function __construct(NotificationAccessorInterface $notificationAccessor)
27
    {
28
        $this->notificationAccessor = $notificationAccessor;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function checkNotification(string $message, NotificationType $type): void
35
    {
36
        foreach ($this->notificationAccessor->getMessageElements() as $messageElement) {
37
            if (
38
                false !== strpos($messageElement->getText(), $message) &&
39
                $messageElement->hasClass($this->resolveClass($type))
40
            ) {
41
                return;
42
            }
43
        }
44
45
        throw new NotificationExpectationMismatchException($type, $message);
46
    }
47
48
    private function resolveClass(NotificationType $type): string
49
    {
50
        $typeClassMap = [
51
            'failure' => 'negative',
52
            'info' => 'info',
53
            'success' => 'positive',
54
        ];
55
56
        Assert::keyExists($typeClassMap, $type->__toString());
57
58
        return $typeClassMap[$type->__toString()];
59
    }
60
}
61