Skip to content

Commit 68bc7f4

Browse files
committed
Add abstract class Provider as base class for SyncProvider
1 parent f3ec19d commit 68bc7f4

File tree

2 files changed

+98
-62
lines changed

2 files changed

+98
-62
lines changed

src/Concept/Provider.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lkrms\Concept;
4+
5+
use Lkrms\Contract\IContainer;
6+
use Lkrms\Contract\IProvider;
7+
use Lkrms\Exception\MethodNotImplementedException;
8+
use Lkrms\Support\DateFormatter;
9+
use Lkrms\Utility\Env;
10+
11+
/**
12+
* Base class for providers
13+
*
14+
*/
15+
abstract class Provider implements IProvider
16+
{
17+
protected IContainer $App;
18+
19+
protected Env $Env;
20+
21+
private DateFormatter $DateFormatter;
22+
23+
/**
24+
* Creates a new provider object
25+
*/
26+
public function __construct(IContainer $app, Env $env)
27+
{
28+
$this->App = $app;
29+
$this->Env = $env;
30+
}
31+
32+
/**
33+
* Get a DateFormatter to work with the backend's date format and/or
34+
* timezone
35+
*
36+
* The {@see DateFormatter} returned will be cached for the lifetime of the
37+
* {@see Provider} instance.
38+
*/
39+
abstract protected function getDateFormatter(): DateFormatter;
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function description(): ?string
45+
{
46+
return null;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function checkHeartbeat(int $ttl = 300)
53+
{
54+
throw new MethodNotImplementedException(
55+
static::class,
56+
__FUNCTION__,
57+
IProvider::class
58+
);
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
final public function app(): IContainer
65+
{
66+
return $this->App;
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
final public function container(): IContainer
73+
{
74+
return $this->App;
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
final public function env(): Env
81+
{
82+
return $this->Env;
83+
}
84+
85+
/**
86+
* @inheritDoc
87+
*/
88+
final public function dateFormatter(): DateFormatter
89+
{
90+
return $this->DateFormatter
91+
?? ($this->DateFormatter = $this->getDateFormatter());
92+
}
93+
}

src/Sync/Concept/SyncProvider.php

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Lkrms\Sync\Concept;
44

5-
use Lkrms\Container\Container;
5+
use Lkrms\Concept\Provider;
6+
use Lkrms\Contract\IContainer;
67
use Lkrms\Contract\IPipeline;
78
use Lkrms\Contract\IService;
8-
use Lkrms\Support\DateFormatter;
99
use Lkrms\Support\Pipeline;
1010
use Lkrms\Sync\Contract\ISyncContext;
11-
use Lkrms\Sync\Contract\ISyncDefinition;
1211
use Lkrms\Sync\Contract\ISyncEntity;
1312
use Lkrms\Sync\Contract\ISyncProvider;
1413
use Lkrms\Sync\Support\SyncContext;
@@ -24,16 +23,8 @@
2423
* Base class for providers that sync entities to and from third-party backends
2524
*
2625
*/
27-
abstract class SyncProvider implements ISyncProvider, IService
26+
abstract class SyncProvider extends Provider implements ISyncProvider, IService
2827
{
29-
/**
30-
* Get a DateFormatter to work with the backend's date format and timezone
31-
*
32-
* The {@see DateFormatter} returned by this method is cached for the
33-
* lifetime of the {@see SyncProvider} instance.
34-
*/
35-
abstract protected function getDateFormatter(): DateFormatter;
36-
3728
/**
3829
* Get a dependency subtitution map for the class
3930
*
@@ -59,24 +50,6 @@ public static function getContextualBindings(): array
5950
return [];
6051
}
6152

62-
/**
63-
* @inheritDoc
64-
*/
65-
public function description(): ?string
66-
{
67-
return null;
68-
}
69-
70-
/**
71-
* @var Container
72-
*/
73-
protected $App;
74-
75-
/**
76-
* @var Env
77-
*/
78-
protected $Env;
79-
8053
/**
8154
* @var SyncStore
8255
*/
@@ -87,20 +60,14 @@ public function description(): ?string
8760
*/
8861
private $Id;
8962

90-
/**
91-
* @var DateFormatter|null
92-
*/
93-
private $DateFormatter;
94-
9563
/**
9664
* @var array<string,Closure>
9765
*/
9866
private $MagicMethodClosures = [];
9967

100-
public function __construct(Container $app, Env $environment, SyncStore $store)
68+
public function __construct(IContainer $app, Env $env, SyncStore $store)
10169
{
102-
$this->App = $app;
103-
$this->Env = $environment;
70+
parent::__construct($app, $env);
10471
$this->Store = $store;
10572

10673
$this->Store->provider($this);
@@ -113,21 +80,6 @@ final public function setProviderId(int $providerId)
11380
return $this;
11481
}
11582

116-
final public function app(): Container
117-
{
118-
return $this->App;
119-
}
120-
121-
final public function container(): Container
122-
{
123-
return $this->App;
124-
}
125-
126-
final public function env(): Env
127-
{
128-
return $this->Env;
129-
}
130-
13183
final public function store(): SyncStore
13284
{
13385
return $this->Store;
@@ -156,15 +108,6 @@ final protected function buildSerializeRules(string $entity): SerializeRulesBuil
156108
->entity($entity);
157109
}
158110

159-
/**
160-
* @inheritDoc
161-
*/
162-
final public function dateFormatter(): DateFormatter
163-
{
164-
return $this->DateFormatter
165-
?? ($this->DateFormatter = $this->getDateFormatter());
166-
}
167-
168111
final public static function getServices(): array
169112
{
170113
return SyncIntrospector::get(static::class)->getSyncProviderInterfaces();

0 commit comments

Comments
 (0)