FlashMessages::info()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 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
use Test\Slick\WebStack\Infrastructure\Http\FlashMessagesTest;
18
use Test\Slick\WebStack\Infrastructure\Http\FlashMessageStorageTest;
19
20
/**
21
 * FlashMessages
22
 *
23
 * @package Slick\WebStack\Infrastructure\Http
24
 * @phpstan-ignore trait.unused
25
 */
26
trait FlashMessages
27
{
28
    protected ?FlashMessageStorage $flashMessageStorage = null;
29
30
    /**
31
     * Sets the flash message storage.
32
     *
33
     * @param FlashMessageStorage $flashMessageStorage The flash message storage instance to set.
34
     * @return FlashMessageStorageTest|FlashMessages|FlashMessagesTest Returns the current object instance.
35
     */
36
    #[Autowire]
37
    public function withMessagesStorage(FlashMessageStorage $flashMessageStorage): self
38
    {
39
        $this->flashMessageStorage = $flashMessageStorage;
40
        return $this;
41
    }
42
43
    /**
44
     * Adds a success flash message and adds it to the flash message storage.
45
     *
46
     * @param string $message The success message to add.
47
     * @return FlashMessageInterface Returns the created flash message.
48
     */
49
    public function success(string $message): FlashMessageInterface
50
    {
51
        $flashMessage = new Message($message, FlashMessageType::SUCCESS);
52
        $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

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