Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
23.08% covered (danger)
23.08%
3 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
HasCommandLogger
23.08% covered (danger)
23.08%
3 / 13
0.00% covered (danger)
0.00%
0 / 1
11.28
0.00% covered (danger)
0.00%
0 / 1
 getLogger
23.08% covered (danger)
23.08%
3 / 13
0.00% covered (danger)
0.00%
0 / 1
11.28
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 LogicException;
23use Psr\Log\LoggerInterface;
24
25/**
26 * Resolves the logger expected by command result helper traits.
27 *
28 * The consuming command is expected to expose an initialized `$logger`
29 * property so reusable traits can log without coupling themselves to a
30 * specific constructor signature.
31 */
32trait HasCommandLogger
33{
34    /**
35     * Returns the logger configured on the consuming command.
36     *
37     * @throws LogicException when the consuming command does not expose a valid logger property
38     */
39    public function getLogger(): LoggerInterface
40    {
41        if (! property_exists($this, 'logger') || null === $this->logger) {
42            throw new LogicException(\sprintf(
43                'Commands using %s MUST expose an initialized $logger property with an instance of %s.',
44                LogsCommandResults::class,
45                LoggerInterface::class,
46            ));
47        }
48
49        if (! $this->logger instanceof LoggerInterface) {
50            throw new LogicException(\sprintf(
51                'Commands using %s MUST expose a %s instance on the $logger property.',
52                LogsCommandResults::class,
53                LoggerInterface::class,
54            ));
55        }
56
57        return $this->logger;
58    }
59}