NikolaGavric94/laravel-square

View on GitHub
src/builders/CustomerBuilder.php

Summary

Maintainability
A
0 mins
Test Coverage
B
88%
<?php
/**
 * Created by PhpStorm.
 * User: nikola
 * Date: 7/6/18
 * Time: 00:06.
 */

namespace Nikolag\Square\Builders;

use Nikolag\Square\Exceptions\MissingPropertyException;
use Nikolag\Square\Models\Customer;
use Nikolag\Square\Utils\Constants;

class CustomerBuilder
{
    /**
     * @var string
     */
    protected string $customerClass = Constants::CUSTOMER_NAMESPACE;

    /**
     * Find or create tax models
     * from taxes array.
     *
     * @param  array  $data
     * @return Customer $temp
     *
     * @throws MissingPropertyException
     */
    public function load(array $data): Customer
    {
        $temp = new $this->customerClass;
        //If email doesn't exist on the customer
        //throw new exception because it should exist
        if (! array_key_exists('email', $data) || $data['email'] == null) {
            throw new MissingPropertyException('$email property for object Customer is missing or is null', 500);
        }

        $query = $temp->newQuery()->where('email', $data['email']);

        if ($query->exists()) {
            $temp = $query->first();
        } else {
            $temp->fill($data);
        }

        return $temp;
    }
}