Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
HasCommandLogger
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
10.40
0.00% covered (danger)
0.00%
0 / 1
 getLogger
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
10.40
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Console\Command\Traits;
21
22use FastForward\DevTools\Console\Command\LoggerAwareCommandInterface;
23use LogicException;
24use Psr\Log\LoggerInterface;
25
26/**
27 * Resolves the logger expected by command result helper traits.
28 *
29 * The consuming command is expected to expose an initialized `$logger`
30 * property so reusable traits can log without coupling themselves to a
31 * specific constructor signature.
32 */
33trait HasCommandLogger
34{
35    /**
36     * Returns the logger configured on the consuming command.
37     *
38     * @throws LogicException when the consuming command does not expose a valid logger property
39     */
40    public function getLogger(): LoggerInterface
41    {
42        if (
43            ! $this instanceof LoggerAwareCommandInterface
44            || (! property_exists($this, 'logger') || null === $this->logger)
45            || ! $this->logger instanceof LoggerInterface
46        ) {
47            throw new LogicException(\sprintf(
48                'Commands using %s MUST implement %s and expose an initialized $logger property with an instance of %s.',
49                LogsCommandResults::class,
50                LoggerAwareCommandInterface::class,
51                LoggerInterface::class,
52            ));
53        }
54
55        return $this->logger;
56    }
57}