Passed
Push — v9 ( e84ae2...80c47f )
by Georges
03:02
created

ExtendedCacheItemPoolTrait::saveMultiple()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Core\Pool;
18
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Event\Event;
21
use Phpfastcache\Event\EventReferenceParameter;
22
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
23
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
24
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
25
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
26
use Psr\Cache\CacheItemInterface;
27
28
trait ExtendedCacheItemPoolTrait
29
{
30
    use CacheItemPoolTrait;
31
32
    /**
33
     * @inheritDoc
34
     * @throws PhpfastcacheCoreException
35
     * @throws PhpfastcacheDriverException
36
     * @throws PhpfastcacheInvalidArgumentException
37
     * @throws PhpfastcacheLogicException
38
     */
39
    public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string
40
    {
41
        return \json_encode(
42
            \array_map(
43
                static fn(CacheItemInterface $item) => $item->get(),
44
                \array_values($this->getItems($keys))
45
            ),
46
            $options,
47
            $depth
48
        );
49
    }
50
51
    public function detachAllItems(): static
52
    {
53
        foreach ($this->itemInstances as $item) {
54
            $this->detachItem($item);
55
        }
56
57
        return $this;
58
    }
59
60
    public function detachItem(CacheItemInterface $item): static
61
    {
62
        if (isset($this->itemInstances[$item->getKey()])) {
63
            $this->deregisterItem($item->getKey());
64
        }
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param ExtendedCacheItemInterface ...$items
71
     * @return bool
72
     * @throws PhpfastcacheCoreException
73
     * @throws PhpfastcacheDriverException
74
     * @throws PhpfastcacheInvalidArgumentException
75
     * @throws PhpfastcacheLogicException
76
     */
77
    public function saveMultiple(ExtendedCacheItemInterface ...$items): bool
78
    {
79
        $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items));
80
81
        if (\count($items)) {
82
            foreach ($items as $item) {
83
                $this->save($item);
84
            }
85
            return true;
86
        }
87
        return false;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getHelp(): string
94
    {
95
        return '';
96
    }
97
}
98