FormatterTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 11
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A formatDataForDisplay() 0 21 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\SimpleCounter.
7
 *
8
 * (c) Eric Sizemore <https://github.com/ericsizemore>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
15
namespace Esi\SimpleCounter\Trait;
16
17
use Esi\SimpleCounter\Interface\ConfigurationInterface;
18
19
use function array_map;
20
use function implode;
21
use function number_format;
22
use function str_split;
23
24
use const DIRECTORY_SEPARATOR;
25
26
/**
27
 * Trait used in *Storage classes.
28
 */
29
trait FormatterTrait
30
{
31
    /**
32
     * Returns the formatted count information given the current count.
33
     *
34
     * If the 'asImage' option is set to true, then HTML is returned. Otherwise,
35
     * just plain text.
36
     *
37
     * Normally called in the Storage class' display() method.
38
     */
39 7
    protected function formatDataForDisplay(ConfigurationInterface $configuration, int $currentCount): string
40
    {
41
        /** @var string $imageDir */
42 7
        $imageDir = $configuration::getOption('imageDir');
43
44
        /** @var string $imageExt */
45 7
        $imageExt = $configuration::getOption('imageExt');
46
47
        /** @var string $visitorTextString */
48 7
        $visitorTextString = $configuration::getOption('visitorTextString');
49
50 7
        if ($configuration::getOption('asImage') === true) {
51 1
            return implode('&nbsp;', array_map(static fn (string $number): string => \sprintf(
52 1
                '<img src="%s%d%s" alt="%2$d" />',
53 1
                $imageDir . DIRECTORY_SEPARATOR,
54 1
                $number,
55 1
                $imageExt,
56 1
            ), str_split((string) $currentCount)));
57
        }
58
59 6
        return \sprintf($visitorTextString, number_format((float) $currentCount));
60
    }
61
}
62