Skip to content
Customer Handling Library for Laravel
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
src
tests
.gitattributes
.gitignore
.styleci.yml
.travis.yml
Changelog.md
LICENSE.md
README.md
composer.json
phpunit.xml.dist

README.md

Konekt Customer Library (Laravel)

Travis Build Status Packagist version Packagist downloads StyleCI MIT Software License

This is a Concord module for handling customers.

Designed to:

  • Be very much Laravel common-sense;
  • Only does the basic shit;
  • Lets you to customize the hell out of it.

Installation

composer require konekt/customer

With Concord

The service provider should be wired via Concord.

Add the customer module to `config/concord.php:

return [
    'modules' => [
        Konekt\Customer\Providers\ModuleServiceProvider::class => []
        // other modules if necessary
    ]
];

If there's no concord.php config file use this command:

php artisan vendor:publish --provider="Konekt\Concord\ConcordServiceProvider" --tag=config

Run Migrations

php artisan migrate

Without Concord

OK, ok. You might be freaked out of this Concord thing. Then you can omit registering the service provider, but then the migrations won't be published, so copy them from src/resorces/database/migrations/ to your app's migration folder.

Usage

Creating Customers

Just go the plain old Laravel way:

use Konekt\Customer\Models\Customer;
use Konekt\Customer\Models\CustomerType;

$johnDoe = Customer::create([
    'firstname' => 'John',
    'lastname'  => 'Doe',
    'type'      => CustomerType::INDIVIDUAL
]);

echo $johnDoe->getName();
// John Doe
var_dump($johnDoe->type->isIndividual());
// bool(true)
var_dump($johnDoe->type->isOrganization());
// bool(false)

$acmeInc = Customer::create([
   'company_name' => 'Acme Inc.',
   'type'      => CustomerType::ORGANIZATION
]);

echo $acmeInc->getName();
// Acme Inc.
var_dump($acmeInc->type->isIndividual());
// bool(false)
var_dump($acmeInc->type->isOrganization());
// bool(true)

Fields

Name Type Notes
id autoinc
type CustomerType enum
email string
phone string(22)
firstname string
lastname string
company_name string
tax_nr string(17) Tax/VAT Identification Number
registration_nr Company/Trade Registration Number
is_active bool true by default
last_purchase_at DateTime nullable

Enums

CustomerType:

Const Value Notes
ORGANIZATION organization (default) Use for companies, foundations, GOs, NGOs, etc
INDIVIDUAL individual For natural human customers

Extending, Customizing

With Concord

Everything is already written in Concord docs at the Models and the Enums sections, so read that first if you haven't done that yet.

Extending CustomerType

This is an enum type so if you want to add further variants, extend the class and define new consts:

// App\CustomerType.php
class CustomerType extends Konekt\Customer\Models\CustomerType
{
    const ROBOT = 'robot';
    const ALIEN = 'alien';
    
    // You can also change the default value:
    const __default = self::ROBOT;
}

You need to register your type so that it will be used instead of the stock CustomerType:

// app/Providers/AppServiceProvider.php:

use Illuminate\Support\ServiceProvider;
use Konekt\Customer\Contracts\CustomerType as CustomerTypeContract;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->concord->registerEnum(
            CustomerTypeContract::class, \App\CustomerType::class
        );
    }
}

So now your extended type is in effect:

use Konekt\Customer\Models\Customer;

$robot = Customer::create([
    'firstname' => 'R2D2'
]);

var_dump($robot->type);
// object(App\CustomerType)#1050 (1) {
//     ["value":protected]=>
//     string(5) "robot"
//   }

Extending Customer

First create your extended model class

// app/Customer.php
class Customer extends Konekt\Customer\Models\Customer
{
    public function getName()
    {
        if ($this->type->isRobot()) {
            return 'Robot ' . $this->firstname;
        }
        
        return parent::getName();        
    }
}

Register the model with concord:

// app/Providers/AppServiceProvider.php:

use Illuminate\Support\ServiceProvider;
use Konekt\Customer\Contracts\Customer as CustomerContract;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->concord->registerModel(
            CustomerContract::class, \App\Customer::class
        );
    }
}

So now any other library using the Konekt Customer module will be aware of your new type and it will be used.

use Konekt\Customer\Models\CustomerProxy;

echo CustomerProxy::modelClass();
// App\Customer

$r2d2 = CustomerProxy::create([
    'firstname' => 'R2D2'
]);
//=> App\Customer {#1062
//       updated_at: "2017-11-26 14:48:48",
//       created_at: "2017-11-26 14:48:48",
//       id: 8,
//       type: "robot",
//       firstname: "R2D2",
//     }

echo $r2d2->getName();
// 'Robot R2D2'

Without Concord

If you're not using Concord and no other module/library depends on this module, just extend Customer with plain OOP and you're good to go.

Regarding to the automatic enum mapping refer to the enum-eloquent readme

Runnig Tests

In Console

vendor/bin/phpunit -c phpunit.xml

Configure PhpStorm

  1. Go to settings -> PHP -> Testing frameworks
  2. Add PHPUnit local
  3. Choose composer autoloader (need to install deps in advance with composer install)
  4. Point "path to script" to vendor/autoload.php (full path)
  5. Choose default configuration file: phpunit.xml
You can’t perform that action at this time.