Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
60.00% covered (warning)
60.00%
3 / 5
CRAP
82.61% covered (success)
82.61%
19 / 23
AbstractSender
0.00% covered (danger)
0.00%
0 / 1
60.00% covered (warning)
60.00%
3 / 5
6.19
82.61% covered (success)
82.61%
19 / 23
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 getLocalDB
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getDbConnectorConfig
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 handler
0.00% covered (danger)
0.00%
0 / 1
2.02
83.33% covered (success)
83.33%
10 / 12
 prepare
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 sync
n/a
0 / 0
0
n/a
0 / 0
 localSync
n/a
0 / 0
0
n/a
0 / 0
1<?php
2
3namespace Qmp\Laravel\Tracker\Services\DeployData\Senders;
4
5
6use Illuminate\Database\Eloquent\Collection;
7use Illuminate\Support\Facades\DB;
8use Illuminate\Support\Facades\Log;
9use Qmp\Laravel\Tracker\Models\Config;
10use Qmp\Laravel\DBConnector\Connectors\MongoConnector;
11use Qmp\Laravel\DBConnector\Connectors\RedisConnector;
12use Qmp\Laravel\DBConnector\Config as DBConnectorConfig;
13
14abstract class AbstractSender implements SenderInterface
15{
16    /**
17     * @var RedisConnector
18     */
19    protected $redis;
20
21    /**
22     * @var MongoConnector
23     */
24    protected $mongo;
25
26    /**
27     * @var Collection
28     */
29    protected $configCollection;
30
31    /**
32     * Undocumented variable
33     *
34     * @var [type]
35     */
36    protected $localMongo;
37
38    /**
39     * Undocumented variable
40     *
41     * @var [type]
42     */
43    protected $data;
44
45    /**
46     * Undocumented variable
47     *
48     * @var [type]
49     */
50    protected $collection;
51
52    /**
53     * Undocumented variable
54     *
55     * @var string
56     */
57    const LOCAL_CONNECTION = "localTracker";
58
59    /**
60     * AbstractSender constructor.
61     * @param Collection $config
62     */
63    public function __construct(Collection $config)
64    {
65        $this->redis = new RedisConnector();
66        $this->mongo = new MongoConnector();
67        $this->configCollection = $config;
68        $this->localMongo = self::getLocalDB();
69    }
70
71    /**
72     * Undocumented function
73     *
74     * @return \Illuminate\Database\ConnectionInterface
75     */
76    public static function getLocalDB() {
77        return DB::connection(self::LOCAL_CONNECTION);
78    }
79
80    /**
81     * Undocumented function
82     *
83     * @param [type] $config
84     * @return DBConnectorConfig
85     */
86    public static function getDbConnectorConfig($config) {
87        $mongoConfig = new DBConnectorConfig($config->mongo);
88        $mongoConfig->host = $config->ip; 
89
90        return $mongoConfig;
91    }
92
93    /**
94     * @param array $data
95     */
96    public function handler(array $data)
97    {
98        Log::debug("Sync $this->collection to local_tracker");
99        $this->prepare($data)->localSync();
100        Log::debug("End sync $this->collection to local_tracker");
101
102        $this->configCollection->each(function (Config $config) use ($data) {
103            try {
104               // $mongoConfig = new DBConnectorConfig($config->mongo);
105               // $mongoConfig->host = $config->ip;
106   
107                $this->mongo->connection(self::getDbConnectorConfig($config));
108
109                /*
110                $redisConfig = new DBConnectorConfig($config->redis);
111                $redisConfig->host = $config->ip;
112                $this->redis->connection($redisConfig);
113                */
114                Log::debug("Sync $this->collection to tracker $config->name");
115                $this->sync();
116                Log::debug("End sync $this->collection to tracker $config->name");
117
118            } catch (\Exception $e) {
119                Log::debug('Unable to connect to server config !:' . $e->getMessage());
120                // TODO ADD replay logic if sync failed ! 
121            }
122        });
123    }
124
125    /**
126     * Undocumented function
127     *d
128     * @param array $data
129     * @return self
130     */
131    protected function prepare(array $data)
132    {
133        $this->data = $data;
134        return $this;
135    }
136
137
138    /**
139     * @return mixed
140     */
141    abstract protected function sync();
142
143    /**
144     * @return mixed
145     */
146    abstract protected function localSync();
147}