Skip to content

Commit

Permalink
Merge pull request #270 from fenos/develop
Browse files Browse the repository at this point in the history
v4.1.0
  • Loading branch information
Gummibeer authored Feb 13, 2017
2 parents 16e7d72 + a5f0cf7 commit 0ec9d4e
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 67 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ addons:
## Services used by this package
services:
- mysql
- postgresql

## List all PHP versions to test with
php:
Expand All @@ -36,12 +37,22 @@ env:
matrix:
fast_finish: true
include:
## php5.5 support build
- php: 5.5
env: LARAVEL_VERSION="5.1.*"
## mysql laravel lts build
- php: 5.6
env: LARAVEL_VERSION="5.1.*" DB_TYPE="mysql"
## mysql laravel latest build
- php: 7.1
env: LARAVEL_VERSION="5.4.*" DB_TYPE="mysql"
## pgsql laravel lts build
- php: 5.6
env: LARAVEL_VERSION="5.1.*" DB_TYPE="pgsql"
## pgsql laravel latest build
- php: 7.1
env: LARAVEL_VERSION="5.4.*" DB_TYPE="pgsql"
## laravel future build
- php: 7.1
env: LARAVEL_VERSION="dev-master"
allow_failures:
Expand All @@ -52,6 +63,7 @@ matrix:
## Run Scripts before Install
before_install:
- mysql -e 'CREATE DATABASE IF NOT EXISTS notifynder;'
- psql -c 'create database notifynder;' -U postgres

## Install Dependencies
install:
Expand Down
14 changes: 0 additions & 14 deletions src/Notifynder/Collections/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Arr;
use InvalidArgumentException;
use Fenos\Notifynder\Models\Notification;
use Fenos\Notifynder\Contracts\ConfigContract;

/**
Expand Down Expand Up @@ -49,19 +48,6 @@ public function isTranslated()
return (bool) $this->get('translation.enabled');
}

/**
* @return string
*/
public function getNotificationModel()
{
$class = $this->get('notification_model');
if (class_exists($class)) {
return $class;
}

return Notification::class;
}

/**
* @return string
* @throws InvalidArgumentException
Expand Down
5 changes: 0 additions & 5 deletions src/Notifynder/Contracts/ConfigContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public function isStrict();
*/
public function isTranslated();

/**
* @return string
*/
public function getNotificationModel();

/**
* @return string
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Notifynder/Helpers/TypeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Traversable;
use Carbon\Carbon;
use InvalidArgumentException;
use Fenos\Notifynder\Models\Notification;

/**
* Class TypeChecker.
Expand Down Expand Up @@ -115,7 +116,7 @@ public static function isIterable($value, $strict = true)
*/
public static function isNotification($notification, $strict = true)
{
if (! is_a($notification, notifynder_config()->getNotificationModel())) {
if (! is_a($notification, app('notifynder.resolver.model')->getModel(Notification::class))) {
if ($strict) {
throw new InvalidArgumentException('The value passed must be an Notification Model instance');
}
Expand Down
5 changes: 5 additions & 0 deletions src/Notifynder/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class Notification extends Model
*/
public function __construct($attributes = [])
{
$table = app('notifynder.resolver.model')->getTable(get_class($this));
if (! empty($table)) {
$this->setTable($table);
}

$this->fillable($this->mergeFillables());

if ($attributes instanceof BuilderNotification) {
Expand Down
8 changes: 6 additions & 2 deletions src/Notifynder/Models/NotificationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class NotificationCategory extends Model

public function __construct(array $attributes = [])
{
$table = app('notifynder.resolver.model')->getTable(get_class($this));
if (! empty($table)) {
$this->setTable($table);
}

$attributes = array_merge([
'text' => '',
], $attributes);
Expand All @@ -54,8 +59,7 @@ public function __construct(array $attributes = [])
*/
public function notifications()
{
$config = app('notifynder.config');
$model = $config->getNotificationModel();
$model = app('notifynder.resolver.model')->getModel(Notification::class);

return $this->hasMany($model, 'category_id');
}
Expand Down
14 changes: 14 additions & 0 deletions src/Notifynder/NotifynderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Fenos\Notifynder\Senders\SingleSender;
use Fenos\Notifynder\Managers\SenderManager;
use Fenos\Notifynder\Senders\MultipleSender;
use Fenos\Notifynder\Resolvers\ModelResolver;
use Fenos\Notifynder\Contracts\ConfigContract;
use Fenos\Notifynder\Managers\NotifynderManager;
use Fenos\Notifynder\Contracts\SenderManagerContract;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function register()
$this->bindContracts();
$this->bindConfig();
$this->bindSender();
$this->bindResolver();
$this->bindNotifynder();

$this->registerSenders();
Expand Down Expand Up @@ -94,6 +96,18 @@ protected function bindSender()
});
}

/**
* Bind Notifynder resolver.
*
* @return void
*/
protected function bindResolver()
{
$this->app->singleton('notifynder.resolver.model', function () {
return new ModelResolver();
});
}

/**
* Bind Notifynder manager.
*
Expand Down
49 changes: 49 additions & 0 deletions src/Notifynder/Resolvers/ModelResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Fenos\Notifynder\Resolvers;

use Illuminate\Support\Str;

class ModelResolver
{
protected $models = [];
protected $tables = [];

public function setModel($class, $model)
{
$this->models[$class] = $model;
}

public function setTable($class, $table)
{
$this->tables[$class] = $table;
}

public function getModel($class)
{
if (isset($this->models[$class])) {
return $this->models[$class];
}

return $class;
}

public function getTable($class)
{
if (isset($this->tables[$class])) {
return $this->tables[$class];
}

return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getModel($class)))));
}

public function make($class, array $attributes = [])
{
$model = $this->getModel($class);
if (! class_exists($model)) {
throw new \ReflectionException("Class {$model} does not exist");
}

return new $model($attributes);
}
}
3 changes: 2 additions & 1 deletion src/Notifynder/Senders/MultipleSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fenos\Notifynder\Senders;

use Fenos\Notifynder\Models\Notification;
use Fenos\Notifynder\Contracts\SenderContract;
use Fenos\Notifynder\Contracts\SenderManagerContract;

Expand Down Expand Up @@ -39,7 +40,7 @@ public function __construct(array $notifications)
*/
public function send(SenderManagerContract $sender)
{
$model = notifynder_config()->getNotificationModel();
$model = app('notifynder.resolver.model')->getModel(Notification::class);
$table = (new $model())->getTable();

$this->database->beginTransaction();
Expand Down
3 changes: 2 additions & 1 deletion src/Notifynder/Senders/OnceSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Fenos\Notifynder\Contracts\SenderContract;
use Fenos\Notifynder\Contracts\SenderManagerContract;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Fenos\Notifynder\Models\Notification as NotificationModel;

/**
* Class OnceSender.
Expand Down Expand Up @@ -80,7 +81,7 @@ protected function getQuery(Notification $notification)
*/
protected function getQueryInstance()
{
$model = notifynder_config()->getNotificationModel();
$model = app('notifynder.resolver.model')->getModel(NotificationModel::class);
$query = $model::query();
if (! ($query instanceof EloquentBuilder)) {
throw new BadMethodCallException("The query method hasn't return an instance of [".EloquentBuilder::class.'].');
Expand Down
3 changes: 2 additions & 1 deletion src/Notifynder/Senders/SingleSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fenos\Notifynder\Senders;

use Fenos\Notifynder\Models\Notification;
use Fenos\Notifynder\Contracts\SenderContract;
use Fenos\Notifynder\Contracts\SenderManagerContract;

Expand Down Expand Up @@ -33,7 +34,7 @@ public function __construct(array $notifications)
*/
public function send(SenderManagerContract $sender)
{
$model = notifynder_config()->getNotificationModel();
$model = app('notifynder.resolver.model')->getModel(Notification::class);

$notification = new $model($this->notification);

Expand Down
4 changes: 3 additions & 1 deletion src/Notifynder/Traits/Notifable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fenos\Notifynder\Traits;

use Fenos\Notifynder\Models\Notification;

/**
* Class Notifable.
*/
Expand All @@ -16,7 +18,7 @@ trait Notifable
*/
public function notifications()
{
$model = notifynder_config()->getNotificationModel();
$model = app('notifynder.resolver.model')->getModel(Notification::class);
if (notifynder_config()->isPolymorphic()) {
return $this->morphMany($model, 'to');
}
Expand Down
4 changes: 3 additions & 1 deletion src/Notifynder/Traits/NotifableLaravel53.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fenos\Notifynder\Traits;

use Fenos\Notifynder\Models\Notification;

/**
* Class Notifable.
*/
Expand All @@ -16,7 +18,7 @@ trait NotifableLaravel53
*/
public function notifynderNotifications()
{
$model = notifynder_config()->getNotificationModel();
$model = app('notifynder.resolver.model')->getModel(Notification::class);
if (notifynder_config()->isPolymorphic()) {
return $this->morphMany($model, 'to');
}
Expand Down
8 changes: 0 additions & 8 deletions src/config/notifynder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
*/
'polymorphic' => false,

/*
* If you need to extend the model class of
* Notifynder you just need to change this line
* With the path / NameSpace of your model and extend it
* with Fenos\Notifynder\Models\Notification
*/
'notification_model' => \Fenos\Notifynder\Models\Notification::class,

/*
* Coordinating a lots notifications that require extra params
* might cause to forget and not insert the {extra.*} value needed.
Expand Down
22 changes: 22 additions & 0 deletions tests/NotifynderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,30 @@ protected function getEnvironmentSetUp($app)
'strict' => false,
'engine' => null,
]);
$app['config']->set('database.connections.test_pgsql', [
'driver' => 'pgsql',
'host' => 'localhost',
'port' => 5432,
'database' => 'notifynder',
'username' => 'postgres',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
]);
if (env('DB_TYPE', 'sqlite') == 'mysql') {
$app['config']->set('database.default', 'test_mysql');
} elseif (env('DB_TYPE', 'sqlite') == 'pgsql') {
$app['config']->set('database.default', 'test_pgsql');
} else {
$app['config']->set('database.default', 'test_sqlite');
}
}

public function tearDown()
{
$resolver = app('notifynder.resolver.model');
$resolver->setTable(Notification::class, 'notifications');
app('db')->rollback();
if (app('db')->getDriverName() == 'mysql') {
app('db')->statement('SET FOREIGN_KEY_CHECKS=0;');
Expand Down Expand Up @@ -157,4 +172,11 @@ protected function getLaravelVersion()

return ($parts[0].'.'.$parts[1]) * 1;
}

public function __call($name, $arguments)
{
if ($name == 'expectException') {
$this->setExpectedException($arguments[0]);
}
}
}
6 changes: 3 additions & 3 deletions tests/integration/Builder/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function testCreateSingleNotificationAndGetArray()

public function testCreateSingleUnvalidNotification()
{
$this->setExpectedException(UnvalidNotificationException::class);
$this->expectException(UnvalidNotificationException::class);

$builder = new Builder();
$builder
Expand Down Expand Up @@ -182,7 +182,7 @@ public function testCreateMultipleNotifications()

public function testCreateMultipleUnvalidNotifications()
{
$this->setExpectedException(UnvalidNotificationException::class);
$this->expectException(UnvalidNotificationException::class);

$builder = new Builder();
$builder->loop([2, 3, 4], function ($builder, $data) {
Expand Down Expand Up @@ -224,7 +224,7 @@ public function testCreateSingleNotificationWithAdditionalField()

public function testCreateSingleUnvalidNotificationWithRequiredField()
{
$this->setExpectedException(UnvalidNotificationException::class);
$this->expectException(UnvalidNotificationException::class);

notifynder_config()->set('additional_fields.required', ['required_field']);

Expand Down
Loading

0 comments on commit 0ec9d4e

Please sign in to comment.