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

grueneschweiz / mailchimpservice / 17108989360

20 Aug 2025 08:00PM UTC coverage: 68.598% (-1.6%) from 70.247%
17108989360

push

github

Michael-Schaer
[FEAT] Sync from mailchimp to crm in specific cases

197 of 333 new or added lines in 10 files covered. (59.16%)

4 existing lines in 2 files now uncovered.

959 of 1398 relevant lines covered (68.6%)

11.04 hits per line

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

1.92
/app/Console/Commands/Sync.php
1
<?php
2

3
namespace App\Console\Commands;
4

5
use App\Exceptions\ConfigException;
6
use App\Exceptions\ParseCrmDataException;
7
use App\Synchronizer\CrmToMailchimpSynchronizer;
8
use App\Synchronizer\MailchimpToCrmCronSynchronizer;
9
use Illuminate\Console\Command;
10

11
class Sync extends Command
12
{
13
    private const DIRECTION_MAILCHIMP = 'toMailchimp';
14
    private const DIRECTION_CRM = 'toCrm';
15

16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'sync:all
22
                            {direction : Possible values are "' . self::DIRECTION_CRM . '" and "' . self::DIRECTION_MAILCHIMP . '".}
23
                            {config : The name of the config file to use.}
24
                            {--limit=100 : How may records should be synchronized at a time.}
25
                            {--offset=0 : How many records should be skipped. Usually used in combination with --limit.}
26
                            {--all : Ignore revision and sync all records, not just changes.}
27
                            {--force : Ignore locks of previously started (running or dead) sync processes.}';
28

29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Synchronize all relevant records from the crm to mailchimp and vice versa.';
35

36
    /**
37
     * Create a new command instance.
38
     *
39
     * @return void
40
     */
41
    public function __construct()
42
    {
43
        parent::__construct();
1✔
44
    }
45

46
    /**
47
     * Execute the console command.
48
     *
49
     * @return integer
50
     *
51
     * @throws \Exception
52
     */
53
    public function handle()
54
    {
55
        switch ($this->argument('direction')) {
×
56
            case self::DIRECTION_CRM:
57
                return $this->syncToCrm();
×
58
            case self::DIRECTION_MAILCHIMP:
59
                return $this->syncToMailchimp();
×
60
            default:
61
                $this->error('Invalid direction argument.');
×
62
                $this->info("Possible values are:\n - " . self::DIRECTION_MAILCHIMP . "\n - " . self::DIRECTION_CRM);
×
63

64
                return 1;
×
65
        }
66
    }
67

68
    /**
69
     * Sync changes from mailchimp to the crm
70
     *
71
     * @return int
72
     * 
73
     * @throws \Exception
74
     */
75
    private function syncToCrm()
76
    {
NEW
77
        $limit = $this->option('limit');
×
NEW
78
        $offset = $this->option('offset');
×
79

NEW
80
        if (!is_numeric($limit) || (int)$limit <= 0) {
×
NEW
81
            $this->error('The limit option must pass an integer > 0.');
×
82

NEW
83
            return 1;
×
84
        }
85

NEW
86
        if (!is_numeric($offset) || (int)$offset < 0) {
×
NEW
87
            $this->error('The offset option must pass an integer >= 0.');
×
88

NEW
89
            return 1;
×
90
        }
91

92
        try {
NEW
93
            $sync = new MailchimpToCrmCronSynchronizer($this->argument('config'));
×
94

NEW
95
            $this->info('Starting Mailchimp to CRM synchronization...');
×
NEW
96
            $result = $sync->syncAll((int)$limit, (int)$offset);
×
NEW
97
            $this->info("Synchronization completed: {$result['processed']} processed, {$result['success']} successful, {$result['failed']} failed");
×
98

NEW
99
            return 0;
×
NEW
100
        } catch (\App\Exceptions\ConfigException $e) {
×
NEW
101
            $this->error('Configuration error: ' . $e->getMessage());
×
NEW
102
            return 1;
×
NEW
103
        } catch (\Exception $e) {
×
NEW
104
            $this->error('Error during synchronization: ' . $e->getMessage());
×
NEW
105
            return 1;
×
106
        }
107
    }
108

109
    /**
110
     * Sync crm records to mailchimp
111
     *
112
     * @return int
113
     *
114
     * @throws \Exception
115
     */
116
    private function syncToMailchimp()
117
    {
118
        $limit = $this->option('limit');
×
119
        $offset = $this->option('offset');
×
120
        $all = $this->option('all');
×
121
        $force = $this->option('force');
×
122

123
        if (!is_numeric($limit) || (int)$limit <= 0) {
×
124
            $this->error('The limit option must pass an integer > 0.');
×
125

126
            return 1;
×
127
        }
128

129
        if (!is_numeric($offset) || (int)$offset < 0) {
×
130
            $this->error('The offset option must pass an integer >= 0.');
×
131

132
            return 1;
×
133
        }
134

135
        try {
136
            $sync = new CrmToMailchimpSynchronizer($this->argument('config'));
×
137

138
            if ($force) {
×
139
                $this->info('Force sync -> removing locks if present.');
×
140
                $sync->unlock();
×
141
            }
142

143
            $this->info('Syncing... please be patient!');
×
144

145
            try {
146
                $sync->syncAllChanges((int)$limit, (int)$offset, (bool)$all); // this is the relevant line! the rest is error handling...
×
147
            } catch (ParseCrmDataException $e) {
×
148
                $this->error('ParseCrmDataException: ' . $e->getMessage());
×
149
                $this->error($e->getFile() . ' on line ' . $e->getLine() . "\n" . $e->getTraceAsString(), 'v');
×
150
            } catch (\Exception $e) {
×
151
                $this->error($e->getMessage());
×
152
                $this->error($e->getFile() . ' on line ' . $e->getLine() . "\n" . $e->getTraceAsString(), 'v');
×
153
            }
UNCOV
154
        } catch (ConfigException $e) {
×
155
            $this->error('ConfigException: ' . $e->getMessage());
×
156
            $this->error($e->getFile() . ' on line ' . $e->getLine() . "\n" . $e->getTraceAsString(), 'v');
×
157
        }
158

159
        return 0;
×
160
    }
161
}
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