Passed
Branch master (372b2a)
by Filipe
01:32
created

FlashMessages   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 1
b 0
f 0
dl 0
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A warning() 0 5 1
A error() 0 5 1
A withMessagesStorage() 0 5 1
A info() 0 5 1
A success() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure\Http;
13
14
use Slick\Di\Definition\Attributes\Autowire;
15
use Slick\WebStack\Infrastructure\Http\FlashMessage\FlashMessageType;
16
use Slick\WebStack\Infrastructure\Http\FlashMessage\Message;
17
18
/**
19
 * FlashMessages
20
 *
21
 * @package Slick\WebStack\Infrastructure\Http
22
 */
23
trait FlashMessages
24
{
25
    protected ?FlashMessageStorage $flashMessageStorage = null;
26
27
    /**
28
     * Sets the flash message storage.
29
     *
30
     * @param FlashMessageStorage $flashMessageStorage The flash message storage instance to set.
31
     * @return self Returns the current object instance.
32
     */
33
    #[Autowire]
34
    public function withMessagesStorage(FlashMessageStorage $flashMessageStorage): self
35
    {
36
        $this->flashMessageStorage = $flashMessageStorage;
37
        return $this;
38
    }
39
40
    /**
41
     * Adds a success flash message and adds it to the flash message storage.
42
     *
43
     * @param string $message The success message to add.
44
     * @return FlashMessageInterface Returns the created flash message.
45
     */
46
    public function success(string $message): FlashMessageInterface
47
    {
48
        $flashMessage = new Message($message, FlashMessageType::SUCCESS);
49
        $this->flashMessageStorage->addMessage($flashMessage);
0 ignored issues
show
Bug introduced by
The method addMessage() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->flashMessageStorage->/** @scrutinizer ignore-call */ 
50
                                    addMessage($flashMessage);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
        return $flashMessage;
51
    }
52
53
    /**
54
     * Create an info flash message and add it to the storage
55
     *
56
     * @param string $message The message to be displayed
57
     * @return FlashMessageInterface The created flash message
58
     */
59
    public function info(string $message): FlashMessageInterface
60
    {
61
        $flashMessage = new Message($message, FlashMessageType::INFO);
62
        $this->flashMessageStorage->addMessage($flashMessage);
63
        return $flashMessage;
64
    }
65
66
    /**
67
     * Generates a warning flash message and adds it to the flash message storage.
68
     *
69
     * @param string $message The message to be displayed in the warning flash message.
70
     * @return FlashMessageInterface The generated warning flash message.
71
     */
72
    public function warning(string $message): FlashMessageInterface
73
    {
74
        $flashMessage = new Message($message, FlashMessageType::WARNING);
75
        $this->flashMessageStorage->addMessage($flashMessage);
76
        return $flashMessage;
77
    }
78
79
    /**
80
     * Generates an error flash message and adds it to the flash message storage.
81
     *
82
     * @param string $message The message to be displayed in the error flash message.
83
     * @return FlashMessageInterface The generated error flash message.
84
     */
85
    public function error(string $message): FlashMessageInterface
86
    {
87
        $flashErrorMessage = new Message($message, FlashMessageType::ERROR);
88
        $this->flashMessageStorage->addMessage($flashErrorMessage);
89
        return $flashErrorMessage;
90
    }
91
}
92