Skip to content

Commit 87f8eb1

Browse files
author
Dominik Liebler
committed
refactored Registry pattern
1 parent b3f144c commit 87f8eb1

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

Structural/Registry/Registry.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ abstract class Registry
1010
* this introduces global state in your application which can not be mocked up for testing
1111
* and is therefor considered an anti-pattern! Use dependency injection instead!
1212
*
13-
* @var array
13+
* @var Service[]
1414
*/
15-
private static $storedValues = [];
15+
private static $services = [];
1616

1717
/**
1818
* @var array
@@ -21,32 +21,21 @@ abstract class Registry
2121
self::LOGGER,
2222
];
2323

24-
/**
25-
* @param string $key
26-
* @param mixed $value
27-
*
28-
* @return void
29-
*/
30-
public static function set(string $key, $value)
24+
public static function set(string $key, Service $value)
3125
{
3226
if (!in_array($key, self::$allowedKeys)) {
3327
throw new \InvalidArgumentException('Invalid key given');
3428
}
3529

36-
self::$storedValues[$key] = $value;
30+
self::$services[$key] = $value;
3731
}
3832

39-
/**
40-
* @param string $key
41-
*
42-
* @return mixed
43-
*/
44-
public static function get(string $key)
33+
public static function get(string $key): Service
4534
{
46-
if (!in_array($key, self::$allowedKeys) || !isset(self::$storedValues[$key])) {
35+
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
4736
throw new \InvalidArgumentException('Invalid key given');
4837
}
4938

50-
return self::$storedValues[$key];
39+
return self::$services[$key];
5140
}
5241
}

Structural/Registry/Service.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
4+
namespace DesignPatterns\Structural\Registry;
5+
6+
7+
class Service {
8+
9+
}

Structural/Registry/Tests/RegistryTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,34 @@
33
namespace DesignPatterns\Structural\Registry\Tests;
44

55
use DesignPatterns\Structural\Registry\Registry;
6-
use stdClass;
6+
use DesignPatterns\Structural\Registry\Service;
7+
use PHPUnit\Framework\MockObject\MockObject;
78
use PHPUnit\Framework\TestCase;
89

910
class RegistryTest extends TestCase
1011
{
11-
public function testSetAndGetLogger()
12+
/**
13+
* @var Service|MockObject
14+
*/
15+
private $service;
16+
17+
protected function setUp(): void
1218
{
13-
$key = Registry::LOGGER;
14-
$logger = new stdClass();
19+
$this->service = $this->getMockBuilder(Service::class)->getMock();
20+
}
1521

16-
Registry::set($key, $logger);
17-
$storedLogger = Registry::get($key);
22+
public function testSetAndGetLogger()
23+
{
24+
Registry::set(Registry::LOGGER, $this->service);
1825

19-
$this->assertSame($logger, $storedLogger);
20-
$this->assertInstanceOf(stdClass::class, $storedLogger);
26+
$this->assertSame($this->service, Registry::get(Registry::LOGGER));
2127
}
2228

2329
public function testThrowsExceptionWhenTryingToSetInvalidKey()
2430
{
2531
$this->expectException(\InvalidArgumentException::class);
2632

27-
Registry::set('foobar', new stdClass());
33+
Registry::set('foobar', $this->service);
2834
}
2935

3036
/**

Structural/Registry/uml/uml.png

15.6 KB
Loading

0 commit comments

Comments
 (0)