HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Mailchimp/Mailchimp.php
<?php

namespace App\Komma\Mailchimp;

use App\Komma\Mailchimp\Types\ListMember;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class Mailchimp
{
    /** @var string */
    private $apiKey;

    /** @var string */
    private $apiEndpoint;

    /** @var Client */
    private $mailchimp;

    /**
     * Make the mailchimp client
     * Mailchimp constructor.
     */
    public function __construct()
    {
        $this->apiKey = config('services.mailchimp.key');

        // The last of the api will be used to asign the <dc> (Data Center)
        $this->apiEndpoint = 'https://'.collect(explode('-', config('services.mailchimp.key')))->last().'.api.mailchimp.com/3.0/';

        $this->mailchimp = new Client([
            'base_uri' => $this->apiEndpoint,
            'auth'     => ['apikey', $this->apiKey],
        ]);
    }

    /**
     * Handles the request through the client.
     * Will also trigger a Error on Warning level to notify us in Slack.
     *
     * @param string $method
     * @param string $uri
     * @param array $options
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    private function request(string $method, string $uri = '', array $options = [])
    {
        try {
            $response = $this->mailchimp->request($method, $uri, $options);

            return json_decode($response->getBody()->getContents());
        } catch (\Exception $exception) {
            if ($exception->getCode() == 400) {
                // Get the body of the exception
                $exceptionResponse = json_decode($exception->getResponse()->getBody()->getContents());

                // Don't log the warning
                if ($exceptionResponse->title !== 'Member Exists') {
                    Log::warning($exception);
                }
            } else {
                Log::warning($exception);
            }

            return null;
        }
    }

    /**
     * Get information about all lists.
     *
     * @link https://mailchimp.com/developer/reference/lists/
     *
     * @return mixed
     */
    public function getLists()
    {
        return $this->request('get', 'lists?count=100');
    }

    /**
     * Get information about a specific list.
     *
     * @link https://mailchimp.com/developer/reference/lists/
     *
     * @param string $listId
     * @return mixed
     */
    public function getList(string $listId)
    {
        return $this->request('get', 'lists/'.$listId);
    }

    /**
     * Add a new list member
     * Default use the one defined in the config, but with optional parameter to overrule the listId
     *
     * @link https://mailchimp.com/developer/reference/lists/list-members/
     *
     * @param ListMember $listMember
     * @param string $listId
     * @return mixed
     */
    public function addNewListMember(ListMember $listMember, string $listId = null)
    {
        if (empty($listId)) {
            $listId = config('services.mailchimp.list');
        }

        return $this->request('post', 'lists/'.$listId.'/members', [
            'json' => $listMember->toArray(),
        ]);
    }
}