• 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

66.67
/app/Synchronizer/Mapper/FieldMaps/FieldMapGroup.php
1
<?php
2

3

4
namespace App\Synchronizer\Mapper\FieldMaps;
5

6

7
use App\Exceptions\ConfigException;
8
use App\Exceptions\ParseCrmDataException;
9
use App\Exceptions\ParseMailchimpDataException;
10
use App\Synchronizer\CrmValue;
11
use App\Synchronizer\Mapper\FieldMaps\GroupConditions\GroupConditionFactory;
12
use App\Synchronizer\Mapper\FieldMaps\GroupConditions\GroupConditionInterface;
13

14
/**
15
 * Mapper for the group fields
16
 *
17
 * Note: in the mailchimp api v3 this field is called 'interests'
18
 *
19
 * @package App\Synchronizer\Mapper
20
 */
21
class FieldMapGroup extends FieldMap
22
{
23
    public const MAILCHIMP_PARENT_KEY = 'interests';
24

25
    private string $mailchimpCategoryId;
26

27
    private GroupConditionInterface $condition;
28

29
    /**
30
     * FieldMapMerge constructor.
31
     *
32
     * @param array $config {
33
     *  crmKey: string,
34
     *  mailchimpCategoryId: string,
35
     *  (trueCondition|trueContainsString): string,
36
     *  (falseCondition|trueContainsString): string,
37
     *  sync: {'both', 'toMailchimp'}
38
     * }
39
     *
40
     * @throws ConfigException
41
     */
42
    public function __construct(array $config)
43
    {
44
        parent::__construct($config);
64✔
45

46
        if (empty($config['mailchimpCategoryId'])) {
64✔
47
            throw new ConfigException('Field: Missing mailchimpCategoryId definition');
×
48
        }
49
        $this->mailchimpCategoryId = $config['mailchimpCategoryId'];
64✔
50

51
        $this->condition = GroupConditionFactory::makeFrom($config);
64✔
52
    }
53

54
    /**
55
     * Parse the payload from mailchimp and extract the values for this field
56
     *
57
     * @param array $data the payload from mailchimps API V3
58
     *
59
     * @throws ParseMailchimpDataException
60
     */
61
    public function addMailchimpData(array $data)
62
    {
63
        if (!array_key_exists(self::MAILCHIMP_PARENT_KEY, $data)) {
8✔
64
            throw new ParseMailchimpDataException(sprintf("Missing key '%s'", self::MAILCHIMP_PARENT_KEY));
×
65
        }
66

67
        if (!array_key_exists($this->mailchimpCategoryId, $data[self::MAILCHIMP_PARENT_KEY])) {
8✔
68
            throw new ParseMailchimpDataException(sprintf(
×
69
                "The interest (also called group or category) with the id '%s' does not exist in mailchimp.",
×
70
                $this->mailchimpCategoryId
×
71
            ));
×
72
        }
73

74
        $inGroup = $data[self::MAILCHIMP_PARENT_KEY][$this->mailchimpCategoryId];
8✔
75

76
        $this->condition->setFromBool($inGroup);
8✔
77
    }
78

79
    /**
80
     * Parse the payload from crm and extract the values for this field
81
     *
82
     * @param array $data the payload from the crm api
83
     *
84
     * @throws ParseCrmDataException
85
     */
86
    public function addCrmData(array $data)
87
    {
88
        if (!array_key_exists($this->crmKey, $data)) {
27✔
89
            throw new ParseCrmDataException(sprintf("Missing key '%s'", $this->crmKey));
×
90
        }
91

92
        $searchableString = "";
27✔
93
        if (is_array($data[$this->crmKey])) {
27✔
94
            // the initial comma is needed for distinguishing e.g. "agriculture" and "culture".
NEW
95
            $searchableString = "," . implode(",", $data[$this->crmKey]);
×
96
        } else {
97
            $searchableString = $data[$this->crmKey];
27✔
98
        }
99

100
        $this->condition->setFromCrmData($searchableString);
27✔
101
    }
102

103
    /**
104
     * Get key value pair ready for storing in the crm
105
     *
106
     * @return CrmValue[]
107
     */
108
    function getCrmData(): array
109
    {
110
        return $this->condition->getCrmValue($this->crmKey);
8✔
111
    }
112

113
    /**
114
     * Get key value pair ready for storing in mailchimp
115
     *
116
     * @return array
117
     */
118
    function getMailchimpDataArray()
119
    {
120
        return [$this->mailchimpCategoryId => $this->condition->getMailchimpValue()];
27✔
121
    }
122

123
    /**
124
     * Get the field key, that will hold the data of this field (for mailchimp requests)
125
     *
126
     * @return string
127
     */
128
    function getMailchimpParentKey()
129
    {
130
        return self::MAILCHIMP_PARENT_KEY;
23✔
131
    }
132
}
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