• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

diego-ninja / granite / 16608963009

29 Jul 2025 10:37PM UTC coverage: 50.423%. First build
16608963009

Pull #5

github

web-flow
Merge 43d8840d7 into 6a6caca51
Pull Request #5: code: adds phpstan level max, pint with per and github actions

321 of 632 new or added lines in 77 files covered. (50.79%)

1132 of 2245 relevant lines covered (50.42%)

9.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.03
/src/Mapping/Cache/SharedMappingCache.php
1
<?php
2

3
namespace Ninja\Granite\Mapping\Cache;
4

5
use Ninja\Granite\Mapping\Contracts\MappingCache;
6

7
/**
8
 * Shared in-memory mapping cache (singleton).
9
 */
10
class SharedMappingCache implements MappingCache
11
{
12
    /**
13
     * Singleton instance.
14
     */
15
    private static ?self $instance = null;
16

17
    /**
18
     * Cache storage.
19
     */
20
    private InMemoryMappingCache $cache;
21

22
    /**
23
     * Hit/miss metrics.
24
     */
25
    private int $hits = 0;
26
    private int $misses = 0;
27

28
    /**
29
     * Private constructor to enforce singleton pattern.
30
     */
31
    private function __construct()
×
32
    {
33
        $this->cache = new InMemoryMappingCache();
×
34
    }
35

36
    /**
37
     * Get singleton instance.
38
     *
39
     * @return self Instance
40
     */
41
    public static function getInstance(): self
×
42
    {
NEW
43
        if (null === self::$instance) {
×
44
            self::$instance = new self();
×
45
        }
46

47
        return self::$instance;
×
48
    }
49

50
    /**
51
     * Check if a mapping configuration exists in cache.
52
     *
53
     * @param string $sourceType Source type name
54
     * @param string $destinationType Destination type name
55
     * @return bool Whether the mapping exists in cache
56
     */
57
    public function has(string $sourceType, string $destinationType): bool
11✔
58
    {
59
        return $this->cache->has($sourceType, $destinationType);
11✔
60
    }
61

62
    /**
63
     * Get a mapping configuration from cache.
64
     *
65
     * @param string $sourceType Source type name
66
     * @param string $destinationType Destination type name
67
     * @return array|null Mapping configuration or null if not found
68
     */
69
    public function get(string $sourceType, string $destinationType): ?array
2✔
70
    {
71
        $result = $this->cache->get($sourceType, $destinationType);
2✔
72

73
        if (null !== $result) {
2✔
74
            $this->hits++;
2✔
75
        } else {
76
            $this->misses++;
×
77
        }
78

79
        return $result;
2✔
80
    }
81

82
    /**
83
     * Store a mapping configuration in cache.
84
     *
85
     * @param string $sourceType Source type name
86
     * @param string $destinationType Destination type name
87
     * @param array $config Mapping configuration
88
     * @return void
89
     */
90
    public function put(string $sourceType, string $destinationType, array $config): void
11✔
91
    {
92
        $this->cache->put($sourceType, $destinationType, $config);
11✔
93
    }
94

95
    /**
96
     * Clear all cached mapping configurations.
97
     *
98
     * @return void
99
     */
100
    public function clear(): void
×
101
    {
102
        $this->cache->clear();
×
103
        $this->hits = 0;
×
104
        $this->misses = 0;
×
105
    }
106

107
    /**
108
     * Get cache statistics.
109
     *
110
     * @return array Cache statistics
111
     */
112
    public function getStats(): array
×
113
    {
114
        $total = $this->hits + $this->misses;
×
115
        $hitRate = $total > 0 ? ($this->hits / $total) * 100 : 0;
×
116

117
        return [
×
118
            'hits' => $this->hits,
×
119
            'misses' => $this->misses,
×
120
            'total' => $total,
×
NEW
121
            'hit_rate' => round($hitRate, 2) . '%',
×
122
        ];
×
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc