Skip to content

Commit 66e7de7

Browse files
First commit
0 parents  commit 66e7de7

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

Manager/AddonManager.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace AppBundle\Manager;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Response;
7+
8+
use AppBundle\ZohoSubscription\Addon\AddonApi;
9+
10+
class AddonManager extends DefaultManager
11+
{
12+
/**
13+
* @throws \Exception
14+
*
15+
* @return Response
16+
*/
17+
public function listAllAddons($type = null)
18+
{
19+
// Si le cache est présent et actif
20+
if ($this->redis && true === $this->cache) {
21+
// Si les résultats sont déjà dans le cache
22+
if ($this->redis->exists('addons')) {
23+
return unserialize($this->redis->get('addons'));
24+
} else {
25+
// Sinon on les stockes
26+
$addons = $this->getAllAddons($type);
27+
28+
$this->redis->setex('addons', $this->ttl, serialize($addons));
29+
30+
return $addons;
31+
}
32+
}
33+
34+
return $this->getAllAddons($type);
35+
}
36+
37+
/**
38+
* @throws \Exception
39+
*
40+
* @return Response
41+
*/
42+
public function getAllAddons($type = null)
43+
{
44+
$addonApi = new AddonApi();
45+
$addons = $addonApi->listAllAddons($type)['addons'];
46+
47+
$addons = array_filter($addons, function ($element) {
48+
return $element['status'] == 'active';
49+
});
50+
51+
return $addons;
52+
}
53+
}

Manager/DefaultManager.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace AppBundle\Manager;
4+
5+
class DefaultManager
6+
{
7+
/**
8+
* @var bool
9+
*/
10+
protected $cache;
11+
12+
/**
13+
* @var Client
14+
*/
15+
protected $redis;
16+
17+
/**
18+
* @var int
19+
*/
20+
protected $ttl;
21+
22+
/**
23+
* @param Client $redis Redis client
24+
* @param bool $cache Whether the cache is active or not
25+
* @param int $ttl Time to keep the cache in seconds
26+
*/
27+
public function __construct($cache = false, Client $redis = null, $ttl = 3600)
28+
{
29+
$this->cache = $cache;
30+
$this->redis = $redis;
31+
$this->ttl = $ttl;
32+
}
33+
}

Manager/PlanManager.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace AppBundle\Manager;
4+
5+
use AppBundle\ZohoSubscription\Addon\AddonApi;
6+
use AppBundle\ZohoSubscription\Plan\PlanApi;
7+
use GuzzleHttp\Client;
8+
use GuzzleHttp\Psr7\Response;
9+
10+
class PlanManager extends DefaultManager
11+
{
12+
/**
13+
* @throws \Exception
14+
*
15+
* @return Response
16+
*/
17+
public function listAllPlans()
18+
{
19+
// Si le cache est présent et actif
20+
if ($this->redis && true === $this->cache) {
21+
// Si les résultats sont déjà dans le cache
22+
if ($this->redis->exists('plans')) {
23+
return unserialize($this->redis->get('plans'));
24+
} else {
25+
// Sinon on les stockes
26+
$plans = $this->getAllPlans();
27+
28+
$this->redis->setex('plans', $this->ttl, serialize($plans));
29+
30+
return $plans;
31+
}
32+
}
33+
34+
return $this->getAllPlans();
35+
}
36+
37+
/**
38+
* @throws \Exception
39+
*
40+
* @return Response
41+
*/
42+
public function getAllPlans()
43+
{
44+
// Sinon on les stockes
45+
$planApi = new PlanApi();
46+
$addonApi = new AddonApi();
47+
$plans = $planApi->listAllPlans()['plans'];
48+
49+
$plans = array_filter($plans, function ($element) {
50+
return $element['status'] == 'active';
51+
});
52+
53+
/*foreach ($plans as &$plan) {
54+
$recurringAddons = [];
55+
56+
foreach ($plan['addons'] as $planAddon) {
57+
$addon = $addonApi->getAddon($planAddon['addon_code'])['addon'];
58+
59+
if ($addon['type'] == 'recurring') {
60+
$recurringAddons[] = $addon;
61+
}
62+
}
63+
64+
$plan['addons'] = $recurringAddons;
65+
}*/
66+
67+
return $plans;
68+
}
69+
}

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"storefactory/zoho-subscription-api": "^1.0"
4+
}
5+
}

0 commit comments

Comments
 (0)