├── .codeclimate.yml ├── .coveralls.yml ├── .gitignore ├── .scrutinizer.yml ├── .styleci.yml ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpspec.yml ├── phpunit.xml ├── spec └── .gitkeep ├── src ├── Notifynder │ ├── Builder │ │ ├── Builder.php │ │ └── Notification.php │ ├── Collections │ │ └── Config.php │ ├── Contracts │ │ ├── ConfigContract.php │ │ ├── NotifynderManagerContract.php │ │ ├── SenderContract.php │ │ └── SenderManagerContract.php │ ├── Exceptions │ │ ├── ExtraParamsException.php │ │ └── UnvalidNotificationException.php │ ├── Facades │ │ └── Notifynder.php │ ├── Helpers │ │ ├── TypeChecker.php │ │ └── helpers.php │ ├── Managers │ │ ├── NotifynderManager.php │ │ └── SenderManager.php │ ├── Models │ │ ├── Notification.php │ │ └── NotificationCategory.php │ ├── NotifynderServiceProvider.php │ ├── Parsers │ │ └── NotificationParser.php │ ├── Resolvers │ │ └── ModelResolver.php │ ├── Senders │ │ ├── MultipleSender.php │ │ ├── OnceSender.php │ │ └── SingleSender.php │ └── Traits │ │ ├── Notifable.php │ │ ├── NotifableBasic.php │ │ ├── NotifableLaravel53.php │ │ └── SenderCallback.php ├── config │ └── notifynder.php └── migrations │ ├── 2014_02_10_145728_notification_categories.php │ ├── 2014_08_01_210813_create_notification_groups_table.php │ ├── 2014_08_01_211045_create_notification_category_notification_group_table.php │ ├── 2015_05_05_212549_create_notifications_table.php │ ├── 2015_06_06_211555_add_expire_time_column_to_notification_table.php │ ├── 2015_06_06_211555_change_type_to_extra_in_notifications_table.php │ ├── 2015_06_07_211555_alter_category_name_to_unique.php │ ├── 2016_04_19_200827_make_notification_url_nullable.php │ ├── 2016_05_19_144531_add_stack_id_to_notifications.php │ ├── 2016_07_01_153156_update_version4_notifications_table.php │ └── 2016_11_02_193415_drop_version4_unused_tables.php └── tests ├── .gitkeep ├── NotifynderTestCase.php ├── integration ├── Builder │ ├── BuilderNotificationTest.php │ └── BuilderTest.php ├── Collections │ └── ConfigTest.php ├── Facades │ └── NotifynderFacadeTest.php ├── Helpers │ ├── HelpersTest.php │ └── TypeCheckerTest.php ├── Managers │ ├── NotifynderManagerTest.php │ └── SenderManagerTest.php ├── Models │ └── NotificationTest.php ├── Parsers │ └── NotificationParserTest.php ├── Resolvers │ └── ModelResolverTest.php ├── Senders │ └── OnceSenderTest.php └── Traits │ ├── NotifableEagerLoadingTest.php │ └── NotifableTest.php ├── migrations ├── 2014_08_01_164248_create_users_table.php └── 2016_08_26_100534_create_cars_table.php └── models ├── Car.php ├── CarL53.php ├── FakeModel.php ├── User.php └── UserL53.php /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | engines: 2 | duplication: 3 | enabled: true 4 | config: 5 | languages: 6 | php: 7 | mass_threshold: 40 8 | fixme: 9 | enabled: true 10 | phpmd: 11 | enabled: true 12 | checks: 13 | CleanCode/StaticAccess: 14 | enabled: false 15 | CleanCode/BooleanArgumentFlag: 16 | enabled: false 17 | Design/TooManyPublicMethods: 18 | enabled: false 19 | Design/CouplingBetweenObjects: 20 | enabled: false 21 | ratings: 22 | paths: 23 | - "src/Notifynder/**.php" 24 | exclude_paths: 25 | - tests/ 26 | - spec/ 27 | - src/migrations/ 28 | - src/config/ 29 | -------------------------------------------------------------------------------- /.coveralls.yml: -------------------------------------------------------------------------------- 1 | coverage_clover: build/logs/clover.xml 2 | json_path: build/logs/coveralls-upload.json 3 | service_name: travis-ci 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | /build 4 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | checks: 2 | php: true 3 | filter: 4 | paths: ["src/Notifynder/*"] 5 | excluded_paths: 6 | - tests/* 7 | build: 8 | environment: 9 | php: 7.0.6 10 | dependencies: 11 | after: 12 | - composer dump-autoload -o 13 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | risky: false 4 | 5 | linting: true 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | ## Run on container environment 4 | sudo: false 5 | 6 | ## Cache composer bits 7 | cache: 8 | directories: 9 | - $HOME/.composer/cache 10 | 11 | ## Addons used by this package 12 | addons: 13 | code_climate: 14 | repo_token: ${CC_TOKEN} 15 | 16 | ## Services used by this package 17 | services: 18 | - mysql 19 | - postgresql 20 | 21 | ## List all PHP versions to test with 22 | php: 23 | - 5.6 24 | - 7.0 25 | - 7.1 26 | 27 | ## Define all ENV vars to test with 28 | env: 29 | - LARAVEL_VERSION="5.0.*" 30 | - LARAVEL_VERSION="5.1.*" 31 | - LARAVEL_VERSION="5.2.*" 32 | - LARAVEL_VERSION="5.3.*" 33 | - LARAVEL_VERSION="5.4.*" 34 | 35 | matrix: 36 | fast_finish: true 37 | include: 38 | ## mysql laravel lts build 39 | - php: 5.6 40 | env: LARAVEL_VERSION="5.1.*" DB_TYPE="mysql" 41 | ## mysql laravel latest build 42 | - php: 7.1 43 | env: LARAVEL_VERSION="5.4.*" DB_TYPE="mysql" 44 | ## pgsql laravel lts build 45 | - php: 5.6 46 | env: LARAVEL_VERSION="5.1.*" DB_TYPE="pgsql" 47 | ## pgsql laravel latest build 48 | - php: 7.1 49 | env: LARAVEL_VERSION="5.4.*" DB_TYPE="pgsql" 50 | 51 | ## Run Scripts before Install 52 | before_install: 53 | - mysql -e 'CREATE DATABASE IF NOT EXISTS notifynder;' 54 | - psql -c 'create database notifynder;' -U postgres 55 | 56 | ## Install Dependencies 57 | install: 58 | - composer self-update 59 | - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; 60 | - composer require laravel/framework:${LARAVEL_VERSION} --no-update --no-interaction 61 | - composer install --prefer-dist --no-interaction 62 | 63 | ## Run Scripts before Tests 64 | before_script: 65 | - composer dump-autoload -o 66 | 67 | ## Run test Scripts 68 | script: 69 | - vendor/bin/phpunit 70 | 71 | ## Run Scripts after Tests 72 | after_script: 73 | - vendor/bin/test-reporter 74 | - export CI_BUILD_NUMBER="$TRAVIS_BUILD_NUMBER" 75 | - export CI_PULL_REQUEST="$TRAVIS_PULL_REQUEST" 76 | - export CI_BRANCH="$TRAVIS_BRANCH" 77 | - vendor/bin/coveralls -v 78 | 79 | ## Send Build Notifications to Slack 80 | notifications: 81 | slack: astrotomic:CnF7P2xaZuJTJ4VzNOy6ksDH 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Fabrizio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Notifynder 4 - Laravel 5 2 | 3 | [![GitHub release](https://img.shields.io/github/release/fenos/Notifynder.svg?style=flat-square)](https://github.com/fenos/Notifynder/releases) 4 | [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/fenos/Notifynder/master/LICENSE) 5 | [![GitHub issues](https://img.shields.io/github/issues/fenos/Notifynder.svg?style=flat-square)](https://github.com/fenos/Notifynder/issues) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/fenos/notifynder.svg?style=flat-square)](https://packagist.org/packages/fenos/notifynder) 7 | [![VersionEye](https://www.versioneye.com/user/projects/5878c014a21fa90051522611/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/5878c014a21fa90051522611) 8 | [![SensioLabs Insight](https://img.shields.io/sensiolabs/i/ef2a6768-337d-4a88-ae0b-8a0eb9621bf5.svg?style=flat-square&label=SensioLabs)](https://insight.sensiolabs.com/projects/ef2a6768-337d-4a88-ae0b-8a0eb9621bf5) 9 | 10 | [![Travis branch](https://img.shields.io/travis/fenos/Notifynder/master.svg?style=flat-square&label=TravisCI)](https://travis-ci.org/fenos/Notifynder/branches) 11 | [![StyleCI](https://styleci.io/repos/18425539/shield)](https://styleci.io/repos/18425539) 12 | [![Scrutinizer Build](https://img.shields.io/scrutinizer/build/g/fenos/Notifynder.svg?style=flat-square&label=ScrutinizerCI)](https://scrutinizer-ci.com/g/fenos/Notifynder/?branch=master) 13 | 14 | [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/fenos/Notifynder.svg?style=flat-square)](https://scrutinizer-ci.com/g/fenos/Notifynder/?branch=master) 15 | [![Code Climate](https://img.shields.io/codeclimate/github/fenos/Notifynder.svg?style=flat-square)](https://codeclimate.com/github/fenos/Notifynder) 16 | [![Coveralls](https://img.shields.io/coveralls/fenos/Notifynder.svg?style=flat-square)](https://coveralls.io/github/fenos/Notifynder) 17 | 18 | [![Slack Team](https://img.shields.io/badge/slack-astrotomic-orange.svg?style=flat-square)](https://astrotomic.slack.com) 19 | [![Slack join](https://img.shields.io/badge/slack-join-green.svg?style=social)](https://notifynder.signup.team) 20 | 21 | 22 | Notifynder is designed to manage notifications in a powerful and easy way. With the flexibility that Notifynder offer, It provide a complete API to work with your notifications, such as storing, retrieving, and organise your codebase to handle hundreds of notifications. You get started in a couple of minutes to "enable" notifications in your Laravel Project. 23 | 24 | Compatible DBs: **MySQL** - **PostgreSQL** - **SQLite** 25 | 26 | Documentation: **[Notifynder Docu](http://notifynder.info)** 27 | 28 | ----- 29 | 30 | ## Installation 31 | 32 | ### Step 1 33 | 34 | Add it on your `composer.json` 35 | 36 | ``` 37 | "fenos/notifynder": "^4.0" 38 | ``` 39 | 40 | and run 41 | 42 | ``` 43 | composer update 44 | ``` 45 | 46 | or run 47 | 48 | ``` 49 | composer require fenos/notifynder 50 | ``` 51 | 52 | 53 | ### Step 2 54 | 55 | Add the following string to `config/app.php` 56 | 57 | **Providers array:** 58 | 59 | ``` 60 | Fenos\Notifynder\NotifynderServiceProvider::class, 61 | ``` 62 | 63 | **Aliases array:** 64 | 65 | ``` 66 | 'Notifynder' => Fenos\Notifynder\Facades\Notifynder::class, 67 | ``` 68 | 69 | 70 | ### Step 3 71 | 72 | #### Migration & Config 73 | 74 | Publish the migration as well as the configuration of notifynder with the following command: 75 | 76 | ``` 77 | php artisan vendor:publish --provider="Fenos\Notifynder\NotifynderServiceProvider" 78 | ``` 79 | 80 | Run the migration 81 | 82 | ``` 83 | php artisan migrate 84 | ``` 85 | 86 | ## Senders 87 | 88 | A list of official supported custom senders is in the [Notifynder Doc](http). 89 | 90 | We also have a [collect issue](https://github.com/fenos/Notifynder/issues/242) for all additional senders we plan or already have. 91 | 92 | If you want any more senders or want to provide your own please [create an issue](https://github.com/fenos/Notifynder/issues/new?milestone=Senders). 93 | 94 | ## ToDo 95 | 96 | Tasks we still have to do: 97 | 98 | * add unittests for parser and models 99 | * complete the new documentation 100 | 101 | ## Versioning 102 | 103 | Starting with `v4.0.0` we are following the [Semantic Versioning Standard](http://semver.org). 104 | 105 | ### Summary 106 | 107 | Given a version number `MAJOR`.`MINOR`.`PATCH`, increment the: 108 | 109 | * **MAJOR** version when you make incompatible API changes, 110 | * **MINOR** version when you add functionality in a backwards-compatible manner, and 111 | * **PATCH** version when you make backwards-compatible bug fixes. 112 | 113 | Additional labels for pre-release (`alpha`, `beta`, `rc`) are available as extensions to the `MAJOR`.`MINOR`.`PATCH` format. 114 | 115 | ## Contributors 116 | 117 | Thanks for everyone [who contributed](https://github.com/fenos/Notifynder/graphs/contributors) to Notifynder and a special thanks for the most active contributors 118 | 119 | - [Gummibeer](https://github.com/Gummibeer) 120 | 121 | ## Services 122 | 123 | * [Travis CI](https://travis-ci.org/fenos/Notifynder) 124 | * [Style CI](https://styleci.io/repos/18425539) 125 | * [Code Climate](https://codeclimate.com/github/fenos/Notifynder) 126 | * [Scrutinizer](https://scrutinizer-ci.com/g/fenos/Notifynder) 127 | * [Coveralls](https://coveralls.io/github/fenos/Notifynder) 128 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fenos/notifynder", 3 | "description": "Management system of internal notifications for Laravel 5.*", 4 | "keywords": ["notifications", "laravel"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Fabrizio Fenoglio", 9 | "email": "fabri_feno@yahoo.it", 10 | "role": "Developer" 11 | }, 12 | { 13 | "name": "Tom Witkowski", 14 | "email": "dev.gummibeer@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.5.0", 20 | "illuminate/support": "~5.0", 21 | "doctrine/dbal": "^2.5" 22 | }, 23 | "require-dev": { 24 | "laravel/framework": "~5.0", 25 | "phpunit/phpunit": "~4.0|~5.0", 26 | "laracasts/testdummy": "~2.0", 27 | "orchestra/testbench": "~3.0", 28 | "orchestra/database": "~3.0|3.4.x-dev", 29 | "codeclimate/php-test-reporter": "^0.3.2", 30 | "satooshi/php-coveralls": "^1.0" 31 | }, 32 | "suggest": { 33 | "astrotomic/notifynder-sender-email": "Allows to send notifications as email.", 34 | "astrotomic/notifynder-sender-redis": "Allows to send notifications via Redis (Pub/Sub).", 35 | "astrotomic/notifynder-sender-slack": "Allows to send notifications via Slack.", 36 | "astrotomic/notifynder-sender-messagebird": "Allows to send notifications via MessageBird.", 37 | "astrotomic/notifynder-sender-nexmo": "Allows to send notifications via Nexmo.", 38 | "astrotomic/notifynder-sender-twilio": "Allows to send notifications via Twilio." 39 | }, 40 | "autoload": { 41 | "psr-4": { 42 | "Fenos\\Notifynder\\": "src/Notifynder" 43 | }, 44 | "files": [ 45 | "src/Notifynder/Helpers/helpers.php" 46 | ] 47 | }, 48 | "autoload-dev": { 49 | "classmap": [ 50 | "tests/NotifynderTestCase.php" 51 | ], 52 | "psr-4": { 53 | "Fenos\\Tests\\": "tests/models" 54 | } 55 | }, 56 | "prefer-stable": true 57 | } 58 | -------------------------------------------------------------------------------- /phpspec.yml: -------------------------------------------------------------------------------- 1 | suites: 2 | notifynder_suite: 3 | namespace: Fenos\Notifynder 4 | src_path: src/Notifynder 5 | # psr4_prefix: Fenos\Notifynder 6 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/integration 15 | 16 | 17 | 18 | 19 | ./src/Notifynder 20 | 21 | 22 | ./vendor 23 | ./tests 24 | ./spec 25 | ./build 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /spec/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fenos/Notifynder/d848d0e3a86bbc18b5ac11091c923dc46cf4ab00/spec/.gitkeep -------------------------------------------------------------------------------- /src/Notifynder/Builder/Builder.php: -------------------------------------------------------------------------------- 1 | notification = new Notification(); 34 | } 35 | 36 | /** 37 | * Set the category for this notification. 38 | * 39 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category 40 | * @return $this 41 | */ 42 | public function category($category) 43 | { 44 | $categoryId = NotificationCategory::getIdByCategory($category); 45 | $this->setNotificationData('category_id', $categoryId); 46 | 47 | return $this; 48 | } 49 | 50 | /** 51 | * Set the sender for this notification. 52 | * 53 | * @return $this 54 | */ 55 | public function from() 56 | { 57 | $args = func_get_args(); 58 | $this->setEntityData($args, 'from'); 59 | 60 | return $this; 61 | } 62 | 63 | /** 64 | * Set the sender anonymous for this notification. 65 | * 66 | * @return $this 67 | */ 68 | public function anonymous() 69 | { 70 | $this->setNotificationData('from_type', null); 71 | $this->setNotificationData('from_id', null); 72 | 73 | return $this; 74 | } 75 | 76 | /** 77 | * Set the receiver for this notification. 78 | * 79 | * @return $this 80 | */ 81 | public function to() 82 | { 83 | $args = func_get_args(); 84 | $this->setEntityData($args, 'to'); 85 | 86 | return $this; 87 | } 88 | 89 | /** 90 | * Set the url for this notification. 91 | * 92 | * @param string $url 93 | * @return $this 94 | */ 95 | public function url($url) 96 | { 97 | TypeChecker::isString($url); 98 | $this->setNotificationData('url', $url); 99 | 100 | return $this; 101 | } 102 | 103 | /** 104 | * Set the expire date for this notification. 105 | * 106 | * @param Carbon|\DateTime $datetime 107 | * @return $this 108 | */ 109 | public function expire(Carbon $datetime) 110 | { 111 | TypeChecker::isDate($datetime); 112 | $carbon = new Carbon($datetime); 113 | $this->setNotificationData('expires_at', $carbon); 114 | 115 | return $this; 116 | } 117 | 118 | /** 119 | * Set the extra values for this notification. 120 | * You can extend the existing extras or override them - important for multiple calls of extra() on one notification. 121 | * 122 | * @param array $extra 123 | * @param bool $override 124 | * @return $this 125 | */ 126 | public function extra(array $extra = [], $override = true) 127 | { 128 | TypeChecker::isArray($extra); 129 | if (! $override) { 130 | $extra = array_merge($this->getNotificationData('extra', []), $extra); 131 | } 132 | $this->setNotificationData('extra', $extra); 133 | 134 | return $this; 135 | } 136 | 137 | /** 138 | * Set updated_at and created_at fields. 139 | */ 140 | public function setDates() 141 | { 142 | $date = Carbon::now(); 143 | 144 | $this->setNotificationData('updated_at', $date); 145 | $this->setNotificationData('created_at', $date); 146 | } 147 | 148 | /** 149 | * Set a single field value. 150 | * 151 | * @param string $key 152 | * @param mixed $value 153 | * @return $this 154 | */ 155 | public function setField($key, $value) 156 | { 157 | $additionalFields = notifynder_config()->getAdditionalFields(); 158 | if (in_array($key, $additionalFields)) { 159 | $this->setNotificationData($key, $value); 160 | } 161 | 162 | return $this; 163 | } 164 | 165 | /** 166 | * Set polymorphic model values. 167 | * 168 | * @param array $entity 169 | * @param string $property 170 | */ 171 | protected function setEntityData($entity, $property) 172 | { 173 | if (is_array($entity) && count($entity) == 2) { 174 | TypeChecker::isString($entity[0]); 175 | TypeChecker::isNumeric($entity[1]); 176 | 177 | $type = $entity[0]; 178 | $id = $entity[1]; 179 | } elseif ($entity[0] instanceof Model) { 180 | $type = $entity[0]->getMorphClass(); 181 | $id = $entity[0]->getKey(); 182 | } else { 183 | TypeChecker::isNumeric($entity[0]); 184 | 185 | $type = notifynder_config()->getNotifiedModel(); 186 | $id = $entity[0]; 187 | } 188 | 189 | $this->setNotificationData("{$property}_type", $type); 190 | $this->setNotificationData("{$property}_id", $id); 191 | } 192 | 193 | /** 194 | * Get a single value of this notification. 195 | * 196 | * @param string $key 197 | * @param null|mixed $default 198 | * @return mixed 199 | */ 200 | protected function getNotificationData($key, $default = null) 201 | { 202 | return $this->notification->get($key, $default); 203 | } 204 | 205 | /** 206 | * Set a single value of this notification. 207 | * 208 | * @param string $key 209 | * @param mixed $value 210 | */ 211 | protected function setNotificationData($key, $value) 212 | { 213 | $this->notification->set($key, $value); 214 | } 215 | 216 | /** 217 | * Get the current notification. 218 | * 219 | * @return Notification 220 | * @throws UnvalidNotificationException 221 | */ 222 | public function getNotification() 223 | { 224 | if (! $this->notification->isValid()) { 225 | throw new UnvalidNotificationException($this->notification); 226 | } 227 | 228 | $this->setDates(); 229 | 230 | return $this->notification; 231 | } 232 | 233 | /** 234 | * Add a notification to the notifications array. 235 | * 236 | * @param Notification $notification 237 | */ 238 | public function addNotification(Notification $notification) 239 | { 240 | $this->notifications[] = $notification; 241 | } 242 | 243 | /** 244 | * Get all notifications. 245 | * 246 | * @return array 247 | * @throws UnvalidNotificationException 248 | */ 249 | public function getNotifications() 250 | { 251 | if (count($this->notifications) == 0) { 252 | $this->addNotification($this->getNotification()); 253 | } 254 | 255 | return $this->notifications; 256 | } 257 | 258 | /** 259 | * Loop over data and call the callback with a new Builder instance and the key and value of the iterated data. 260 | * 261 | * @param array|\Traversable $data 262 | * @param Closure $callback 263 | * @return $this 264 | * @throws UnvalidNotificationException 265 | */ 266 | public function loop($data, Closure $callback) 267 | { 268 | TypeChecker::isIterable($data); 269 | 270 | foreach ($data as $key => $value) { 271 | $builder = new static(); 272 | $callback($builder, $value, $key); 273 | $this->addNotification($builder->getNotification()); 274 | } 275 | 276 | return $this; 277 | } 278 | 279 | /** 280 | * @param string $offset 281 | * @return bool 282 | */ 283 | public function offsetExists($offset) 284 | { 285 | return $this->notification->offsetExists($offset); 286 | } 287 | 288 | /** 289 | * @param string $offset 290 | * @return mixed 291 | */ 292 | public function offsetGet($offset) 293 | { 294 | return $this->notification->offsetGet($offset); 295 | } 296 | 297 | /** 298 | * @param string $offset 299 | * @param mixed $value 300 | */ 301 | public function offsetSet($offset, $value) 302 | { 303 | $this->notification->offsetSet($offset, $value); 304 | } 305 | 306 | /** 307 | * @param string $offset 308 | */ 309 | public function offsetUnset($offset) 310 | { 311 | $this->notification->offsetUnset($offset); 312 | } 313 | } 314 | -------------------------------------------------------------------------------- /src/Notifynder/Builder/Notification.php: -------------------------------------------------------------------------------- 1 | getAdditionalRequiredFields(); 38 | $this->requiredFields = array_merge($this->requiredFields, $customRequired); 39 | } 40 | 41 | /** 42 | * @return array 43 | */ 44 | public function attributes() 45 | { 46 | return $this->attributes; 47 | } 48 | 49 | /** 50 | * @param string $key 51 | * @param null|mixed $default 52 | * @return mixed 53 | */ 54 | public function attribute($key, $default = null) 55 | { 56 | return $this->get($key, $default); 57 | } 58 | 59 | /** 60 | * @param string $key 61 | * @return bool 62 | */ 63 | public function has($key) 64 | { 65 | return Arr::has($this->attributes, $key); 66 | } 67 | 68 | /** 69 | * @param string $key 70 | * @param null|mixed $default 71 | * @return mixed 72 | */ 73 | public function get($key, $default = null) 74 | { 75 | return Arr::get($this->attributes, $key, $default); 76 | } 77 | 78 | /** 79 | * @param string $key 80 | * @param mixed $value 81 | */ 82 | public function set($key, $value) 83 | { 84 | Arr::set($this->attributes, $key, $value); 85 | } 86 | 87 | /** 88 | * @return bool 89 | */ 90 | public function isValid() 91 | { 92 | foreach ($this->requiredFields as $field) { 93 | if (! $this->has($field)) { 94 | return false; 95 | } 96 | } 97 | 98 | return true; 99 | } 100 | 101 | /** 102 | * @param string $key 103 | * @return mixed 104 | */ 105 | public function __get($key) 106 | { 107 | return $this->get($key); 108 | } 109 | 110 | /** 111 | * @param string $key 112 | * @param mixed $value 113 | */ 114 | public function __set($key, $value) 115 | { 116 | $this->set($key, $value); 117 | } 118 | 119 | /** 120 | * @param int $options 121 | * @return string 122 | */ 123 | public function toJson($options = 0) 124 | { 125 | return json_encode($this->jsonSerialize(), $options); 126 | } 127 | 128 | /** 129 | * @return array 130 | */ 131 | public function jsonSerialize() 132 | { 133 | return $this->toArray(); 134 | } 135 | 136 | /** 137 | * @return array 138 | */ 139 | public function toArray() 140 | { 141 | return array_map(function ($value) { 142 | return $value instanceof Arrayable ? $value->toArray() : $value; 143 | }, $this->attributes()); 144 | } 145 | 146 | /** 147 | * @return array 148 | */ 149 | public function toDbArray() 150 | { 151 | $notification = $this->toArray(); 152 | if (array_key_exists('extra', $notification) && is_array($notification['extra'])) { 153 | $notification['extra'] = json_encode($notification['extra']); 154 | } 155 | 156 | return $notification; 157 | } 158 | 159 | /** 160 | * @return string 161 | */ 162 | public function getText() 163 | { 164 | if ($this->isValid()) { 165 | $notification = new ModelNotification($this); 166 | $notifynderParse = new NotificationParser(); 167 | 168 | return $notifynderParse->parse($notification); 169 | } 170 | } 171 | 172 | /** 173 | * @return string 174 | */ 175 | public function toString() 176 | { 177 | return $this->toJson(); 178 | } 179 | 180 | /** 181 | * @return string 182 | */ 183 | public function __toString() 184 | { 185 | return $this->toString(); 186 | } 187 | 188 | /** 189 | * @param string $offset 190 | * @return bool 191 | */ 192 | public function offsetExists($offset) 193 | { 194 | return $this->has($offset); 195 | } 196 | 197 | /** 198 | * @param string $offset 199 | * @return mixed 200 | */ 201 | public function offsetGet($offset) 202 | { 203 | return $this->get($offset); 204 | } 205 | 206 | /** 207 | * @param string $offset 208 | * @param mixed $value 209 | */ 210 | public function offsetSet($offset, $value) 211 | { 212 | $this->set($offset, $value); 213 | } 214 | 215 | /** 216 | * @param string $offset 217 | */ 218 | public function offsetUnset($offset) 219 | { 220 | Arr::forget($this->attributes, $offset); 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/Notifynder/Collections/Config.php: -------------------------------------------------------------------------------- 1 | reload(); 25 | } 26 | 27 | /** 28 | * @return bool 29 | */ 30 | public function isPolymorphic() 31 | { 32 | return (bool) $this->get('polymorphic'); 33 | } 34 | 35 | /** 36 | * @return bool 37 | */ 38 | public function isStrict() 39 | { 40 | return (bool) $this->get('strict_extra'); 41 | } 42 | 43 | /** 44 | * @return bool 45 | */ 46 | public function isTranslated() 47 | { 48 | return (bool) $this->get('translation.enabled'); 49 | } 50 | 51 | /** 52 | * @return string 53 | * @throws InvalidArgumentException 54 | */ 55 | public function getNotifiedModel() 56 | { 57 | $class = $this->get('model'); 58 | if (class_exists($class)) { 59 | return $class; 60 | } 61 | throw new InvalidArgumentException("The model class [{$class}] doesn't exist."); 62 | } 63 | 64 | /** 65 | * @return array 66 | */ 67 | public function getAdditionalFields() 68 | { 69 | return Arr::flatten($this->get('additional_fields', [])); 70 | } 71 | 72 | /** 73 | * @return array 74 | */ 75 | public function getAdditionalRequiredFields() 76 | { 77 | return Arr::flatten($this->get('additional_fields.required', [])); 78 | } 79 | 80 | /** 81 | * @return string 82 | */ 83 | public function getTranslationDomain() 84 | { 85 | return $this->get('translation.domain', 'notifynder'); 86 | } 87 | 88 | /** 89 | * @param string $key 90 | * @param null|mixed $default 91 | * @return mixed 92 | */ 93 | public function get($key, $default = null) 94 | { 95 | return Arr::get($this->items, $key, $default); 96 | } 97 | 98 | /** 99 | * @param string $key 100 | * @return bool 101 | */ 102 | public function has($key) 103 | { 104 | return Arr::has($this->items, $key); 105 | } 106 | 107 | /** 108 | * @param string $key 109 | * @param null $value 110 | */ 111 | public function set($key, $value = null) 112 | { 113 | Arr::set($this->items, $key, $value); 114 | app('config')->set('notifynder.'.$key, $value); 115 | } 116 | 117 | /** 118 | * @param string $key 119 | */ 120 | public function forget($key) 121 | { 122 | Arr::forget($this->items, $key); 123 | app('config')->offsetUnset('notifynder.'.$key); 124 | } 125 | 126 | public function reload() 127 | { 128 | $this->items = app('config')->get('notifynder'); 129 | } 130 | 131 | /** 132 | * @param string $key 133 | * @return mixed 134 | */ 135 | public function __get($key) 136 | { 137 | return $this->get($key); 138 | } 139 | 140 | /** 141 | * @param string $key 142 | * @param mixed $value 143 | */ 144 | public function __set($key, $value) 145 | { 146 | $this->set($key, $value); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/Notifynder/Contracts/ConfigContract.php: -------------------------------------------------------------------------------- 1 | notification = $notification; 27 | } 28 | 29 | /** 30 | * @return Notification 31 | */ 32 | public function getNotification() 33 | { 34 | return $this->notification; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Notifynder/Facades/Notifynder.php: -------------------------------------------------------------------------------- 1 | 0) { 101 | return true; 102 | } 103 | 104 | if ($strict) { 105 | throw new InvalidArgumentException('The value passed must be iterable'); 106 | } 107 | 108 | return false; 109 | } 110 | 111 | /** 112 | * @param $notification 113 | * @param bool $strict 114 | * @return bool 115 | * @throws InvalidArgumentException 116 | */ 117 | public static function isNotification($notification, $strict = true) 118 | { 119 | if (! is_a($notification, app('notifynder.resolver.model')->getModel(Notification::class))) { 120 | if ($strict) { 121 | throw new InvalidArgumentException('The value passed must be an Notification Model instance'); 122 | } 123 | 124 | return false; 125 | } 126 | 127 | return true; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/Notifynder/Helpers/helpers.php: -------------------------------------------------------------------------------- 1 | get($key, $default); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Notifynder/Managers/NotifynderManager.php: -------------------------------------------------------------------------------- 1 | sender = $sender; 34 | } 35 | 36 | /** 37 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category 38 | * @return $this 39 | */ 40 | public function category($category) 41 | { 42 | $this->builder(true); 43 | $this->builder->category($category); 44 | 45 | return $this; 46 | } 47 | 48 | /** 49 | * @param array|\Traversable $data 50 | * @param Closure $callback 51 | * @return $this 52 | */ 53 | public function loop($data, Closure $callback) 54 | { 55 | $this->builder(true); 56 | $this->builder->loop($data, $callback); 57 | 58 | return $this; 59 | } 60 | 61 | /** 62 | * @param bool $force 63 | * @return Builder 64 | */ 65 | public function builder($force = false) 66 | { 67 | if (is_null($this->builder) || $force) { 68 | $this->builder = new Builder(); 69 | } 70 | 71 | return $this->builder; 72 | } 73 | 74 | /** 75 | * @return bool 76 | */ 77 | public function send() 78 | { 79 | $sent = $this->sender->send($this->builder->getNotifications()); 80 | $this->reset(); 81 | 82 | return (bool) $sent; 83 | } 84 | 85 | /** 86 | * @return SenderManagerContract 87 | */ 88 | public function sender() 89 | { 90 | return $this->sender; 91 | } 92 | 93 | protected function reset() 94 | { 95 | $this->builder = null; 96 | } 97 | 98 | /** 99 | * @param string $name 100 | * @param Closure $sender 101 | * @return bool 102 | */ 103 | public function extend($name, Closure $sender) 104 | { 105 | return (bool) $this->sender->extend($name, $sender); 106 | } 107 | 108 | /** 109 | * @param $name 110 | * @param $arguments 111 | * @return $this|bool 112 | * @throws BadMethodCallException 113 | */ 114 | public function __call($name, $arguments) 115 | { 116 | if (Str::startsWith($name, 'send')) { 117 | $sent = $this->sender->sendWithCustomSender($name, $this->builder->getNotifications()); 118 | $this->reset(); 119 | 120 | return (bool) $sent; 121 | } 122 | 123 | if ($this->builder instanceof Builder && method_exists($this->builder, $name)) { 124 | $result = call_user_func_array([$this->builder, $name], $arguments); 125 | if (Str::startsWith($name, 'get')) { 126 | return $result; 127 | } 128 | 129 | return $this; 130 | } 131 | 132 | $error = "The method [$name] doesn't exist in the class ".self::class; 133 | throw new BadMethodCallException($error); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/Notifynder/Managers/SenderManager.php: -------------------------------------------------------------------------------- 1 | sendSingle($notifications); 36 | } 37 | 38 | return (bool) $this->sendMultiple($notifications); 39 | } 40 | 41 | /** 42 | * @param string $name 43 | * @return bool 44 | */ 45 | public function hasSender($name) 46 | { 47 | return Arr::has($this->senders, $name); 48 | } 49 | 50 | /** 51 | * @param string $name 52 | * @return Closure 53 | */ 54 | public function getSender($name) 55 | { 56 | return Arr::get($this->senders, $name); 57 | } 58 | 59 | /** 60 | * @param string $name 61 | * @param Closure $sender 62 | * @return bool 63 | */ 64 | public function extend($name, Closure $sender) 65 | { 66 | if (Str::startsWith($name, 'send')) { 67 | $this->senders[$name] = $sender; 68 | 69 | return true; 70 | } 71 | 72 | return false; 73 | } 74 | 75 | /** 76 | * @param string $class 77 | * @param callable $callback 78 | * @return bool 79 | */ 80 | public function setCallback($class, callable $callback) 81 | { 82 | if (class_exists($class)) { 83 | $this->callbacks[$class] = $callback; 84 | 85 | return true; 86 | } 87 | 88 | return false; 89 | } 90 | 91 | /** 92 | * @param string $class 93 | * @return callable|null 94 | */ 95 | public function getCallback($class) 96 | { 97 | return Arr::get($this->callbacks, $class); 98 | } 99 | 100 | /** 101 | * @param string $name 102 | * @param array $notifications 103 | * @return bool 104 | * @throws BadFunctionCallException 105 | * @throws BadMethodCallException 106 | */ 107 | public function sendWithCustomSender($name, array $notifications) 108 | { 109 | if ($this->hasSender($name)) { 110 | $sender = call_user_func_array($this->getSender($name), [$notifications]); 111 | if ($sender instanceof SenderContract) { 112 | return (bool) $sender->send($this); 113 | } 114 | throw new BadFunctionCallException("The sender [{$name}] hasn't returned an instance of ".SenderContract::class); 115 | } 116 | throw new BadMethodCallException("The sender [{$name}] isn't registered."); 117 | } 118 | 119 | /** 120 | * @param string $name 121 | * @param array $arguments 122 | * @return bool 123 | * @throws BadMethodCallException 124 | */ 125 | public function __call($name, array $arguments) 126 | { 127 | if (array_key_exists(0, $arguments) && isset($arguments[0]) && is_array($arguments[0])) { 128 | return $this->sendWithCustomSender($name, $arguments[0]); 129 | } 130 | 131 | throw new BadMethodCallException('No argument passed to the custom sender, please provide notifications array'); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/Notifynder/Models/Notification.php: -------------------------------------------------------------------------------- 1 | 'array', 54 | ]; 55 | 56 | /** 57 | * Notification constructor. 58 | * 59 | * @param array|\Fenos\Notifynder\Builder\Notification $attributes 60 | */ 61 | public function __construct($attributes = []) 62 | { 63 | $table = app('notifynder.resolver.model')->getTable(get_class($this)); 64 | if (! empty($table)) { 65 | $this->setTable($table); 66 | } 67 | 68 | $this->fillable($this->mergeFillables()); 69 | 70 | if ($attributes instanceof BuilderNotification) { 71 | $attributes = $attributes->toArray(); 72 | } 73 | 74 | parent::__construct($attributes); 75 | } 76 | 77 | /** 78 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 79 | */ 80 | public function category() 81 | { 82 | return $this->belongsTo(NotificationCategory::class, 'category_id'); 83 | } 84 | 85 | /** 86 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo 87 | */ 88 | public function from() 89 | { 90 | if (notifynder_config()->isPolymorphic()) { 91 | return $this->morphTo('from'); 92 | } 93 | 94 | return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id'); 95 | } 96 | 97 | /** 98 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo 99 | */ 100 | public function to() 101 | { 102 | if (notifynder_config()->isPolymorphic()) { 103 | return $this->morphTo('to'); 104 | } 105 | 106 | return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id'); 107 | } 108 | 109 | /** 110 | * @return array 111 | */ 112 | public function getCustomFillableFields() 113 | { 114 | return notifynder_config()->getAdditionalFields(); 115 | } 116 | 117 | /** 118 | * @return array 119 | */ 120 | protected function mergeFillables() 121 | { 122 | $fillables = array_unique(array_merge($this->getFillable(), $this->getCustomFillableFields())); 123 | 124 | return $fillables; 125 | } 126 | 127 | /** 128 | * @return string 129 | * @throws \Fenos\Notifynder\Exceptions\ExtraParamsException 130 | */ 131 | public function getTextAttribute() 132 | { 133 | if (! array_key_exists('text', $this->attributes)) { 134 | $notifynderParse = new NotificationParser(); 135 | $this->attributes['text'] = $notifynderParse->parse($this); 136 | } 137 | 138 | return $this->attributes['text']; 139 | } 140 | 141 | /** 142 | * @return bool|int 143 | */ 144 | public function read() 145 | { 146 | return $this->update(['read' => 1]); 147 | } 148 | 149 | /** 150 | * @return bool|int 151 | */ 152 | public function unread() 153 | { 154 | return $this->update(['read' => 0]); 155 | } 156 | 157 | /** 158 | * @return bool 159 | */ 160 | public function resend() 161 | { 162 | $this->updateTimestamps(); 163 | $this->read = 0; 164 | 165 | return $this->save(); 166 | } 167 | 168 | /** 169 | * @return bool 170 | */ 171 | public function isAnonymous() 172 | { 173 | return is_null($this->from_id); 174 | } 175 | 176 | /** 177 | * @param Builder $query 178 | * @param $category 179 | * @return Builder 180 | */ 181 | public function scopeByCategory(Builder $query, $category) 182 | { 183 | $categoryId = NotificationCategory::getIdByCategory($category); 184 | 185 | return $query->where('category_id', $categoryId); 186 | } 187 | 188 | /** 189 | * @param Builder $query 190 | * @param int $read 191 | * @return Builder 192 | */ 193 | public function scopeByRead(Builder $query, $read = 1) 194 | { 195 | return $query->where('read', $read); 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /src/Notifynder/Models/NotificationCategory.php: -------------------------------------------------------------------------------- 1 | getTable(get_class($this)); 46 | if (! empty($table)) { 47 | $this->setTable($table); 48 | } 49 | 50 | $attributes = array_merge([ 51 | 'text' => '', 52 | ], $attributes); 53 | 54 | parent::__construct($attributes); 55 | } 56 | 57 | /** 58 | * @return \Illuminate\Database\Eloquent\Relations\HasMany 59 | */ 60 | public function notifications() 61 | { 62 | $model = app('notifynder.resolver.model')->getModel(Notification::class); 63 | 64 | return $this->hasMany($model, 'category_id'); 65 | } 66 | 67 | public function setNameAttribute($value) 68 | { 69 | $parts = explode('.', $value); 70 | foreach ($parts as $i => $part) { 71 | $parts[$i] = Str::slug(preg_replace('/[^a-z0-9_]/', '_', strtolower($part)), '_'); 72 | } 73 | $this->attributes['name'] = implode('.', $parts); 74 | } 75 | 76 | /** 77 | * @return \Symfony\Component\Translation\TranslatorInterface|string 78 | */ 79 | public function getTemplateBodyAttribute() 80 | { 81 | if (notifynder_config()->isTranslated()) { 82 | $key = notifynder_config()->getTranslationDomain().'.'.$this->name; 83 | $trans = trans($key); 84 | if ($trans != $key) { 85 | return $trans; 86 | } 87 | } 88 | 89 | return $this->text; 90 | } 91 | 92 | /** 93 | * @param Builder $query 94 | * @param $name 95 | * @return Builder 96 | */ 97 | public function scopeByName(Builder $query, $name) 98 | { 99 | return $query->where('name', $name); 100 | } 101 | 102 | /** 103 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category 104 | * @return int 105 | */ 106 | public static function getIdByCategory($category) 107 | { 108 | $categoryId = $category; 109 | if ($category instanceof self) { 110 | $categoryId = $category->getKey(); 111 | } elseif (! is_numeric($category)) { 112 | $categoryId = self::byName($category)->firstOrFail()->getKey(); 113 | } 114 | 115 | return $categoryId; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Notifynder/NotifynderServiceProvider.php: -------------------------------------------------------------------------------- 1 | '2014_02_10_145728_notification_categories', 24 | 'CreateNotificationGroupsTable' => '2014_08_01_210813_create_notification_groups_table', 25 | 'CreateNotificationCategoryNotificationGroupTable' => '2014_08_01_211045_create_notification_category_notification_group_table', 26 | 'CreateNotificationsTable' => '2015_05_05_212549_create_notifications_table', 27 | 'AddExpireTimeColumnToNotificationTable' => '2015_06_06_211555_add_expire_time_column_to_notification_table', 28 | 'ChangeTypeToExtraInNotificationsTable' => '2015_06_06_211555_change_type_to_extra_in_notifications_table', 29 | 'AlterCategoryNameToUnique' => '2015_06_07_211555_alter_category_name_to_unique', 30 | 'MakeNotificationUrlNullable' => '2016_04_19_200827_make_notification_url_nullable', 31 | 'AddStackIdToNotifications' => '2016_05_19_144531_add_stack_id_to_notifications', 32 | 'UpdateVersion4NotificationsTable' => '2016_07_01_153156_update_version4_notifications_table', 33 | 'DropVersion4UnusedTables' => '2016_11_02_193415_drop_version4_unused_tables', 34 | ]; 35 | 36 | /** 37 | * Register the service provider. 38 | * 39 | * @return void 40 | */ 41 | public function register() 42 | { 43 | $this->bindContracts(); 44 | $this->bindConfig(); 45 | $this->bindSender(); 46 | $this->bindResolver(); 47 | $this->bindNotifynder(); 48 | 49 | $this->registerSenders(); 50 | } 51 | 52 | /** 53 | * Boot the service provider. 54 | * 55 | * @return void 56 | */ 57 | public function boot() 58 | { 59 | $this->config(); 60 | $this->migration(); 61 | } 62 | 63 | /** 64 | * Bind contracts. 65 | * 66 | * @return void 67 | */ 68 | protected function bindContracts() 69 | { 70 | $this->app->bind(NotifynderManagerContract::class, 'notifynder'); 71 | $this->app->bind(SenderManagerContract::class, 'notifynder.sender'); 72 | $this->app->bind(ConfigContract::class, 'notifynder.config'); 73 | } 74 | 75 | /** 76 | * Bind Notifynder config. 77 | * 78 | * @return void 79 | */ 80 | protected function bindConfig() 81 | { 82 | $this->app->singleton('notifynder.config', function () { 83 | return new Config(); 84 | }); 85 | } 86 | 87 | /** 88 | * Bind Notifynder sender. 89 | * 90 | * @return void 91 | */ 92 | protected function bindSender() 93 | { 94 | $this->app->singleton('notifynder.sender', function () { 95 | return new SenderManager(); 96 | }); 97 | } 98 | 99 | /** 100 | * Bind Notifynder resolver. 101 | * 102 | * @return void 103 | */ 104 | protected function bindResolver() 105 | { 106 | $this->app->singleton('notifynder.resolver.model', function () { 107 | return new ModelResolver(); 108 | }); 109 | } 110 | 111 | /** 112 | * Bind Notifynder manager. 113 | * 114 | * @return void 115 | */ 116 | protected function bindNotifynder() 117 | { 118 | $this->app->singleton('notifynder', function ($app) { 119 | return new NotifynderManager( 120 | $app['notifynder.sender'] 121 | ); 122 | }); 123 | } 124 | 125 | /** 126 | * Register the default senders. 127 | * 128 | * @return void 129 | */ 130 | public function registerSenders() 131 | { 132 | app('notifynder')->extend('sendSingle', function (array $notifications) { 133 | return new SingleSender($notifications); 134 | }); 135 | 136 | app('notifynder')->extend('sendMultiple', function (array $notifications) { 137 | return new MultipleSender($notifications); 138 | }); 139 | 140 | app('notifynder')->extend('sendOnce', function (array $notifications) { 141 | return new OnceSender($notifications); 142 | }); 143 | } 144 | 145 | /** 146 | * Publish and merge config file. 147 | * 148 | * @return void 149 | */ 150 | protected function config() 151 | { 152 | $this->publishes([ 153 | __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'), 154 | ]); 155 | 156 | $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder'); 157 | } 158 | 159 | /** 160 | * Publish migration files. 161 | * 162 | * @return void 163 | */ 164 | protected function migration() 165 | { 166 | foreach ($this->migrations as $class => $file) { 167 | if (! class_exists($class)) { 168 | $this->publishMigration($file); 169 | } 170 | } 171 | } 172 | 173 | /** 174 | * Publish a single migration file. 175 | * 176 | * @param string $filename 177 | * @return void 178 | */ 179 | protected function publishMigration($filename) 180 | { 181 | $extension = '.php'; 182 | $filename = trim($filename, $extension).$extension; 183 | $stub = __DIR__.'/../migrations/'.$filename; 184 | $target = $this->getMigrationFilepath($filename); 185 | $this->publishes([$stub => $target], 'migrations'); 186 | } 187 | 188 | /** 189 | * Get the migration file path. 190 | * 191 | * @param string $filename 192 | * @return string 193 | */ 194 | protected function getMigrationFilepath($filename) 195 | { 196 | if (function_exists('database_path')) { 197 | return database_path('/migrations/'.$filename); 198 | } 199 | 200 | return base_path('/database/migrations/'.$filename); // @codeCoverageIgnore 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /src/Notifynder/Parsers/NotificationParser.php: -------------------------------------------------------------------------------- 1 | category; 30 | if (is_null($category)) { 31 | throw (new ModelNotFoundException)->setModel( 32 | NotificationCategory::class, $notification->category_id 33 | ); 34 | } 35 | $text = $category->template_body; 36 | 37 | $specialValues = $this->getValues($text); 38 | if (count($specialValues) > 0) { 39 | $specialValues = array_filter($specialValues, function ($value) use ($notification) { 40 | return ((is_array($notification) && isset($notification[$value])) || (is_object($notification) && isset($notification->$value))) || starts_with($value, ['extra.', 'to.', 'from.']); 41 | }); 42 | 43 | foreach ($specialValues as $replacer) { 44 | $replace = $this->mixedGet($notification, $replacer); 45 | if (empty($replace) && notifynder_config()->isStrict()) { 46 | throw new ExtraParamsException("The following [$replacer] param required from your category is missing."); 47 | } 48 | $text = $this->replace($text, $replace, $replacer); 49 | } 50 | } 51 | 52 | return $text; 53 | } 54 | 55 | /** 56 | * Get an array of all placehodlers. 57 | * 58 | * @param string $body 59 | * @return array 60 | */ 61 | protected function getValues($body) 62 | { 63 | $values = []; 64 | preg_match_all(self::RULE, $body, $values); 65 | 66 | return $values[1]; 67 | } 68 | 69 | /** 70 | * Replace a single placeholder. 71 | * 72 | * @param string $body 73 | * @param string $valueMatch 74 | * @param string $replacer 75 | * @return string 76 | */ 77 | protected function replace($body, $valueMatch, $replacer) 78 | { 79 | $body = str_replace('{'.$replacer.'}', $valueMatch, $body); 80 | 81 | return $body; 82 | } 83 | 84 | /** 85 | * @param array|object $object 86 | * @param string $key 87 | * @param null|mixed $default 88 | * @return mixed 89 | */ 90 | protected function mixedGet($object, $key, $default = null) 91 | { 92 | if (is_null($key) || trim($key) == '') { 93 | return ''; 94 | } 95 | foreach (explode('.', $key) as $segment) { 96 | if (is_object($object) && isset($object->{$segment})) { 97 | $object = $object->{$segment}; 98 | continue; 99 | } 100 | if (is_object($object) && method_exists($object, '__get') && ! is_null($object->__get($segment))) { 101 | $object = $object->__get($segment); 102 | continue; 103 | } 104 | if (is_object($object) && method_exists($object, 'getAttribute') && ! is_null($object->getAttribute($segment))) { 105 | $object = $object->getAttribute($segment); 106 | continue; 107 | } 108 | if (is_array($object) && array_key_exists($segment, $object)) { 109 | $object = array_get($object, $segment, $default); 110 | continue; 111 | } 112 | 113 | return value($default); 114 | } 115 | 116 | return $object; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/Notifynder/Resolvers/ModelResolver.php: -------------------------------------------------------------------------------- 1 | models[$class] = $model; 15 | } 16 | 17 | public function setTable($class, $table) 18 | { 19 | $this->tables[$class] = $table; 20 | } 21 | 22 | public function getModel($class) 23 | { 24 | if (isset($this->models[$class])) { 25 | return $this->models[$class]; 26 | } 27 | 28 | return $class; 29 | } 30 | 31 | public function getTable($class) 32 | { 33 | if (isset($this->tables[$class])) { 34 | return $this->tables[$class]; 35 | } 36 | 37 | return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getModel($class))))); 38 | } 39 | 40 | public function make($class, array $attributes = []) 41 | { 42 | $model = $this->getModel($class); 43 | if (! class_exists($model)) { 44 | throw new \ReflectionException("Class {$model} does not exist"); 45 | } 46 | 47 | return new $model($attributes); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Notifynder/Senders/MultipleSender.php: -------------------------------------------------------------------------------- 1 | notifications = $notifications; 32 | $this->database = app('db'); 33 | } 34 | 35 | /** 36 | * Send all notifications. 37 | * 38 | * @param SenderManagerContract $sender 39 | * @return bool 40 | */ 41 | public function send(SenderManagerContract $sender) 42 | { 43 | $model = app('notifynder.resolver.model')->getModel(Notification::class); 44 | $table = (new $model())->getTable(); 45 | 46 | $this->database->beginTransaction(); 47 | $stackId = $this->database 48 | ->table($table) 49 | ->max('stack_id') + 1; 50 | foreach ($this->notifications as $key => $notification) { 51 | $this->notifications[$key] = $this->notifications[$key]->toDbArray(); 52 | $this->notifications[$key]['stack_id'] = $stackId; 53 | } 54 | $insert = $this->database 55 | ->table($table) 56 | ->insert($this->notifications); 57 | $this->database->commit(); 58 | 59 | return (bool) $insert; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Notifynder/Senders/OnceSender.php: -------------------------------------------------------------------------------- 1 | notifications = $notifications; 30 | } 31 | 32 | /** 33 | * Send the notification once. 34 | * 35 | * @param SenderManagerContract $sender 36 | * @return bool 37 | */ 38 | public function send(SenderManagerContract $sender) 39 | { 40 | $success = true; 41 | foreach ($this->notifications as $notification) { 42 | $query = $this->getQuery($notification); 43 | if (! $query->exists()) { 44 | $success = $sender->send([$notification]) ? $success : false; 45 | continue; 46 | } 47 | $success = $query->firstOrFail()->resend() ? $success : false; 48 | } 49 | 50 | return $success; 51 | } 52 | 53 | /** 54 | * Get the base query. 55 | * 56 | * @param Notification $notification 57 | * @return \Illuminate\Database\Eloquent\Builder 58 | */ 59 | protected function getQuery(Notification $notification) 60 | { 61 | $query = $this->getQueryInstance(); 62 | $query 63 | ->where('from_id', $notification->from_id) 64 | ->where('from_type', $notification->from_type) 65 | ->where('to_id', $notification->to_id) 66 | ->where('to_type', $notification->to_type) 67 | ->where('category_id', $notification->category_id); 68 | $extra = $notification->extra; 69 | if (! is_null($extra) && ! empty($extra)) { 70 | if (is_array($extra)) { 71 | $extra = json_encode($extra); 72 | } 73 | $query->where('extra', $extra); 74 | } 75 | 76 | return $query; 77 | } 78 | 79 | /** 80 | * @return \Illuminate\Database\Eloquent\Builder 81 | */ 82 | protected function getQueryInstance() 83 | { 84 | $model = app('notifynder.resolver.model')->getModel(NotificationModel::class); 85 | $query = $model::query(); 86 | if (! ($query instanceof EloquentBuilder)) { 87 | throw new BadMethodCallException("The query method hasn't return an instance of [".EloquentBuilder::class.'].'); 88 | } 89 | 90 | return $query; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/Notifynder/Senders/SingleSender.php: -------------------------------------------------------------------------------- 1 | notification = array_values($notifications)[0]; 27 | } 28 | 29 | /** 30 | * Send the single notification. 31 | * 32 | * @param SenderManagerContract $sender 33 | * @return bool 34 | */ 35 | public function send(SenderManagerContract $sender) 36 | { 37 | $model = app('notifynder.resolver.model')->getModel(Notification::class); 38 | 39 | $notification = new $model($this->notification); 40 | 41 | return $notification->save(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Notifynder/Traits/Notifable.php: -------------------------------------------------------------------------------- 1 | getModel(Notification::class); 44 | if (notifynder_config()->isPolymorphic()) { 45 | return $this->morphMany($model, 'to'); 46 | } 47 | 48 | return $this->hasMany($model, 'to_id'); 49 | } 50 | 51 | /** 52 | * Get the notifications Relationship without any eager loading. 53 | * 54 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany 55 | */ 56 | public function getLazyNotificationRelation() 57 | { 58 | return $this->notifications(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Notifynder/Traits/NotifableBasic.php: -------------------------------------------------------------------------------- 1 | getLazyNotificationRelation()->with($with); 39 | } 40 | 41 | /** 42 | * Get a new NotifynderManager instance with the given category. 43 | * 44 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category 45 | * @return \Fenos\Notifynder\Managers\NotifynderManager 46 | */ 47 | public function notifynder($category) 48 | { 49 | return app('notifynder')->category($category); 50 | } 51 | 52 | /** 53 | * Get a new NotifynderManager instance with the given category and $this as the sender. 54 | * 55 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category 56 | * @return \Fenos\Notifynder\Managers\NotifynderManager 57 | */ 58 | public function sendNotificationFrom($category) 59 | { 60 | return $this->notifynder($category)->from($this); 61 | } 62 | 63 | /** 64 | * Get a new NotifynderManager instance with the given category and $this as the receiver. 65 | * 66 | * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category 67 | * @return \Fenos\Notifynder\Managers\NotifynderManager 68 | */ 69 | public function sendNotificationTo($category) 70 | { 71 | return $this->notifynder($category)->to($this); 72 | } 73 | 74 | /** 75 | * Read a single Notification. 76 | * 77 | * @param int $notification 78 | * @return bool 79 | */ 80 | public function readNotification($notification) 81 | { 82 | return $this->updateSingleReadStatus($notification, 1); 83 | } 84 | 85 | /** 86 | * Unread a single Notification. 87 | * 88 | * @param int $notification 89 | * @return bool 90 | */ 91 | public function unreadNotification($notification) 92 | { 93 | return $this->updateSingleReadStatus($notification, 0); 94 | } 95 | 96 | /** 97 | * @param int $notification 98 | * @param int $value 99 | * @return bool 100 | */ 101 | protected function updateSingleReadStatus($notification, $value) 102 | { 103 | if (! TypeChecker::isNotification($notification, false)) { 104 | $notification = $this->getLazyNotificationRelation()->findOrFail($notification); 105 | } 106 | 107 | if ($this->getLazyNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) { 108 | return $value ? $notification->read() : $notification->unread(); 109 | } 110 | 111 | return false; 112 | } 113 | 114 | /** 115 | * Read all Notifications. 116 | * 117 | * @return mixed 118 | */ 119 | public function readAllNotifications() 120 | { 121 | return $this->getLazyNotificationRelation()->update(['read' => 1]); 122 | } 123 | 124 | /** 125 | * Unread all Notifications. 126 | * 127 | * @return mixed 128 | */ 129 | public function unreadAllNotifications() 130 | { 131 | return $this->getLazyNotificationRelation()->update(['read' => 0]); 132 | } 133 | 134 | /** 135 | * Count unread notifications. 136 | * 137 | * @return int 138 | */ 139 | public function countUnreadNotifications() 140 | { 141 | return $this->getLazyNotificationRelation()->byRead(0)->count(); 142 | } 143 | 144 | /** 145 | * Get all Notifications ordered by creation and optional limit. 146 | * 147 | * @param null|int $limit 148 | * @param string $order 149 | * @return \Illuminate\Database\Eloquent\Collection 150 | */ 151 | public function getNotifications($limit = null, $order = 'desc') 152 | { 153 | $query = $this->getNotificationRelation()->orderBy('created_at', $order); 154 | if (! is_null($limit)) { 155 | $query->limit($limit); 156 | } 157 | 158 | return $query->get(); 159 | } 160 | 161 | /** 162 | * Get all unread Notifications. 163 | * 164 | * @param null|int $limit 165 | * @param string $order 166 | * @return \Illuminate\Database\Eloquent\Collection 167 | */ 168 | public function getNotificationsNotRead($limit = null, $order = 'desc') 169 | { 170 | $query = $this->getNotificationRelation()->byRead(0)->orderBy('created_at', $order); 171 | if (! is_null($limit)) { 172 | $query->limit($limit); 173 | } 174 | 175 | return $query->get(); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/Notifynder/Traits/NotifableLaravel53.php: -------------------------------------------------------------------------------- 1 | getModel(Notification::class); 44 | if (notifynder_config()->isPolymorphic()) { 45 | return $this->morphMany($model, 'to'); 46 | } 47 | 48 | return $this->hasMany($model, 'to_id'); 49 | } 50 | 51 | /** 52 | * Get the notifications Relationship without any eager loading. 53 | * 54 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany 55 | */ 56 | public function getLazyNotificationRelation() 57 | { 58 | return $this->notifynderNotifications(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Notifynder/Traits/SenderCallback.php: -------------------------------------------------------------------------------- 1 | getCallback(get_called_class()); 13 | if (! is_callable($callback)) { 14 | throw new \UnexpectedValueException("The callback isn't callable."); 15 | } 16 | 17 | return $callback; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/config/notifynder.php: -------------------------------------------------------------------------------- 1 | 'App\User', 17 | 18 | /* 19 | * Do you want notifynder to eager load its related models? 20 | * just swap the value to true to eager load relations 21 | * or you can specify which relations to eager load 22 | * 23 | * Possible Values: 24 | * false: No relations are eager loaded (default) 25 | * true: All relations are eager loaded 26 | * array: Specify which relations to eager load can be any combination of ['category', 'from', 'to'] 27 | */ 28 | 'eager_load' => false, 29 | 30 | /* 31 | * Do you want have notifynder that work polymorphically? 32 | * just swap the value to true and you will able to use it! 33 | */ 34 | 'polymorphic' => false, 35 | 36 | /* 37 | * Coordinating a lots notifications that require extra params 38 | * might cause to forget and not insert the {extra.*} value needed. 39 | * This flag allow you to cause an exception to be thrown if you miss 40 | * to store a extra param that the category will need. 41 | * NOTE: use only in development. 42 | * WHEN DISABLED: will just remove the {extra.*} markup from the sentence 43 | */ 44 | 'strict_extra' => false, 45 | 46 | /* 47 | * If you wish to have the translations in a specific file 48 | * just require the file on the following option. 49 | * 50 | * To get started with the translations just reference a key with 51 | * the language you wish to translate ex 'it' or 'italian' and pass as 52 | * value an array with the translations 53 | */ 54 | 'translation' => [ 55 | 'enabled' => false, 56 | 'domain' => 'notifynder', 57 | ], 58 | 59 | /* 60 | * If you have added your own fields to the Notification Model 61 | * you can add them to the arrays below. 62 | * 63 | * If you want them to be required by the builder add them to the 64 | * to the required key - if they are just added you can add them 65 | * to the fillable key. 66 | */ 67 | 'additional_fields' => [ 68 | 'required' => [ 69 | 70 | ], 71 | 'fillable' => [ 72 | 73 | ], 74 | ], 75 | ]; 76 | -------------------------------------------------------------------------------- /src/migrations/2014_02_10_145728_notification_categories.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('name')->index(); 18 | $table->string('text'); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | * 25 | * @return void 26 | */ 27 | public function down() 28 | { 29 | Schema::dropIfExists('notification_categories'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/migrations/2014_08_01_210813_create_notification_groups_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('name', 50)->index()->unique(); 18 | }); 19 | } 20 | 21 | /** 22 | * Reverse the migrations. 23 | * 24 | * @return void 25 | */ 26 | public function down() 27 | { 28 | Schema::dropIfExists('notification_groups'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/migrations/2014_08_01_211045_create_notification_category_notification_group_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->integer('category_id')->unsigned()->index(); 18 | $table->foreign('category_id')->references('id')->on('notification_categories')->onDelete('cascade'); 19 | $table->integer('group_id')->unsigned()->index(); 20 | $table->foreign('group_id')->references('id')->on('notification_groups')->onDelete('cascade'); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::dropIfExists('notifications_categories_in_groups'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/migrations/2015_05_05_212549_create_notifications_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->bigInteger('from_id')->index()->unsigned(); 18 | $table->string('from_type')->index()->nullable(); 19 | $table->bigInteger('to_id')->index()->unsigned(); 20 | $table->string('to_type')->index()->nullable(); 21 | $table->integer('category_id')->index()->unsigned(); 22 | $table->string('url'); 23 | $table->string('extra')->nullable(); 24 | $table->tinyInteger('read')->default(0); 25 | $table->timestamps(); 26 | 27 | $table->foreign('category_id')->references('id') 28 | ->on('notification_categories'); 29 | }); 30 | } 31 | 32 | /** 33 | * Reverse the migrations. 34 | * 35 | * @return void 36 | */ 37 | public function down() 38 | { 39 | Schema::dropIfExists('notifications'); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/migrations/2015_06_06_211555_add_expire_time_column_to_notification_table.php: -------------------------------------------------------------------------------- 1 | timestamp('expire_time')->nullable(); 17 | }); 18 | } 19 | 20 | /** 21 | * Reverse the migrations. 22 | * 23 | * @return void 24 | */ 25 | public function down() 26 | { 27 | Schema::table('notifications', function (Blueprint $table) { 28 | $table->dropColumn('expire_time'); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/migrations/2015_06_06_211555_change_type_to_extra_in_notifications_table.php: -------------------------------------------------------------------------------- 1 | unique('name'); 17 | }); 18 | } 19 | 20 | /** 21 | * Reverse the migrations. 22 | * 23 | * @return void 24 | */ 25 | public function down() 26 | { 27 | Schema::table('notification_categories', function (Blueprint $table) { 28 | $table->dropUnique('notification_categories_name_unique'); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/migrations/2016_04_19_200827_make_notification_url_nullable.php: -------------------------------------------------------------------------------- 1 | string('url')->nullable()->change(); 17 | }); 18 | } 19 | 20 | /** 21 | * Reverse the migrations. 22 | * 23 | * @return void 24 | */ 25 | public function down() 26 | { 27 | Schema::table('notifications', function (Blueprint $table) { 28 | $table->string('url')->change(); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/migrations/2016_05_19_144531_add_stack_id_to_notifications.php: -------------------------------------------------------------------------------- 1 | integer('stack_id')->unsigned()->nullable()->after('expire_time'); 17 | }); 18 | } 19 | 20 | /** 21 | * Reverse the migrations. 22 | * 23 | * @return void 24 | */ 25 | public function down() 26 | { 27 | Schema::table('notifications', function (Blueprint $table) { 28 | $table->dropColumn('stack_id'); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/migrations/2016_07_01_153156_update_version4_notifications_table.php: -------------------------------------------------------------------------------- 1 | bigInteger('from_id')->unsigned()->nullable()->change(); 17 | }); 18 | Schema::table('notifications', function (Blueprint $table) { 19 | $table->renameColumn('expire_time', 'expires_at'); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::table('notifications', function (Blueprint $table) { 31 | $table->renameColumn('expires_at', 'expire_time'); 32 | }); 33 | Schema::table('notifications', function (Blueprint $table) { 34 | $table->bigInteger('from_id')->unsigned()->change(); 35 | }); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/migrations/2016_11_02_193415_drop_version4_unused_tables.php: -------------------------------------------------------------------------------- 1 | NotifynderFacade::class, 27 | ]; 28 | } 29 | 30 | public function setUp() 31 | { 32 | parent::setUp(); 33 | $artisan = $this->app->make('Illuminate\Contracts\Console\Kernel'); 34 | app('db')->beginTransaction(); 35 | $this->migrate($artisan); 36 | $this->migrate($artisan, '/../../../../tests/migrations'); 37 | // Set up the User Test Model 38 | app('config')->set('notifynder.notification_model', 'Fenos\Notifynder\Models\Notification'); 39 | app('config')->set('notifynder.model', 'Fenos\Tests\Models\User'); 40 | } 41 | 42 | protected function getEnvironmentSetUp($app) 43 | { 44 | $app['config']->set('database.connections.test_sqlite', [ 45 | 'driver' => 'sqlite', 46 | 'database' => ':memory:', 47 | 'prefix' => '', 48 | ]); 49 | $app['config']->set('database.connections.test_mysql', [ 50 | 'driver' => 'mysql', 51 | 'host' => '127.0.0.1', 52 | 'port' => 3306, 53 | 'database' => 'notifynder', 54 | 'username' => 'travis', 55 | 'password' => '', 56 | 'charset' => 'utf8', 57 | 'collation' => 'utf8_general_ci', 58 | 'prefix' => '', 59 | 'strict' => false, 60 | 'engine' => null, 61 | ]); 62 | $app['config']->set('database.connections.test_pgsql', [ 63 | 'driver' => 'pgsql', 64 | 'host' => 'localhost', 65 | 'port' => 5432, 66 | 'database' => 'notifynder', 67 | 'username' => 'postgres', 68 | 'password' => '', 69 | 'charset' => 'utf8', 70 | 'prefix' => '', 71 | 'schema' => 'public', 72 | ]); 73 | if (env('DB_TYPE', 'sqlite') == 'mysql') { 74 | $app['config']->set('database.default', 'test_mysql'); 75 | } elseif (env('DB_TYPE', 'sqlite') == 'pgsql') { 76 | $app['config']->set('database.default', 'test_pgsql'); 77 | } else { 78 | $app['config']->set('database.default', 'test_sqlite'); 79 | } 80 | } 81 | 82 | public function tearDown() 83 | { 84 | $resolver = app('notifynder.resolver.model'); 85 | $resolver->setTable(Notification::class, 'notifications'); 86 | app('db')->rollback(); 87 | if (app('db')->getDriverName() == 'mysql') { 88 | app('db')->statement('SET FOREIGN_KEY_CHECKS=0;'); 89 | Notification::truncate(); 90 | NotificationCategory::truncate(); 91 | app('db')->statement('SET FOREIGN_KEY_CHECKS=1;'); 92 | } 93 | } 94 | 95 | protected function getApplicationTimezone($app) 96 | { 97 | return 'UTC'; 98 | } 99 | 100 | protected function migrate($artisan, $path = '/../../../../src/migrations') 101 | { 102 | $artisan->call('migrate', [ 103 | '--path' => $path, 104 | ]); 105 | } 106 | 107 | protected function createCategory(array $attributes = []) 108 | { 109 | $attributes = array_merge([ 110 | 'text' => 'Notification send from #{from.id} to #{to.id}.', 111 | 'name' => 'test.category', 112 | ], $attributes); 113 | 114 | $category = NotificationCategory::byName($attributes['name'])->first(); 115 | if ($category instanceof NotificationCategory) { 116 | return $category; 117 | } 118 | 119 | return NotificationCategory::create($attributes); 120 | } 121 | 122 | protected function createUser(array $attributes = []) 123 | { 124 | $attributes = array_merge([ 125 | 'firstname' => 'John', 126 | 'lastname' => 'Doe', 127 | ], $attributes); 128 | 129 | if ($this->getLaravelVersion() < 5.3) { 130 | return User::create($attributes); 131 | } else { 132 | return UserL53::create($attributes); 133 | } 134 | } 135 | 136 | protected function createCar(array $attributes = []) 137 | { 138 | $attributes = array_merge([ 139 | 'brand' => 'Audi', 140 | 'model' => 'A6', 141 | ], $attributes); 142 | 143 | if ($this->getLaravelVersion() < 5.3) { 144 | return Car::create($attributes); 145 | } else { 146 | return CarL53::create($attributes); 147 | } 148 | } 149 | 150 | protected function sendNotificationTo(Model $model) 151 | { 152 | $category = $this->createCategory(); 153 | 154 | return $model 155 | ->sendNotificationTo($category->getKey()) 156 | ->from(2) 157 | ->send(); 158 | } 159 | 160 | protected function sendNotificationsTo(Model $model, $amount = 10) 161 | { 162 | while ($amount > 0) { 163 | $this->sendNotificationTo($model); 164 | $amount--; 165 | } 166 | } 167 | 168 | protected function getLaravelVersion() 169 | { 170 | $version = app()->version(); 171 | $parts = explode('.', $version); 172 | 173 | return ($parts[0].'.'.$parts[1]) * 1; 174 | } 175 | 176 | public function __call($name, $arguments) 177 | { 178 | if ($name == 'expectException') { 179 | $this->setExpectedException($arguments[0]); 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /tests/integration/Builder/BuilderNotificationTest.php: -------------------------------------------------------------------------------- 1 | set('foo', 'bar'); 11 | 12 | $this->assertInternalType('array', $notification->attributes()); 13 | $this->assertCount(1, $notification->attributes()); 14 | $this->assertArrayHasKey('foo', $notification->attributes()); 15 | $this->assertTrue($notification->has('foo')); 16 | $this->assertSame('bar', $notification->attribute('foo')); 17 | $this->assertFalse($notification->isValid()); 18 | 19 | $notification->set('category_id', 1); 20 | $notification->set('from_id', 1); 21 | $notification->set('to_id', 2); 22 | 23 | $this->assertTrue($notification->isValid()); 24 | } 25 | 26 | public function testTypeChanger() 27 | { 28 | $notification = new Notification(); 29 | $notification->set('category_id', 1); 30 | $notification->set('from_id', 1); 31 | $notification->set('to_id', 2); 32 | $notification->set('extra', ['foo' => 'bar']); 33 | 34 | $this->assertTrue($notification->isValid()); 35 | $this->assertInternalType('array', $notification->toArray()); 36 | $this->assertInternalType('array', $notification->toArray()['extra']); 37 | $this->assertInternalType('array', $notification->toDbArray()); 38 | $this->assertInternalType('string', $notification->toDbArray()['extra']); 39 | $this->assertJson($notification->toJson()); 40 | $this->assertInternalType('string', $notification->toString()); 41 | $this->assertInternalType('string', (string) $notification); 42 | } 43 | 44 | public function testOverloaded() 45 | { 46 | $notification = new Notification(); 47 | $notification->category_id = 1; 48 | $notification->from_id = 1; 49 | $notification->to_id = 2; 50 | 51 | $this->assertTrue($notification->isValid()); 52 | $this->assertSame(1, $notification->category_id); 53 | $this->assertSame(1, $notification->from_id); 54 | $this->assertSame(2, $notification->to_id); 55 | } 56 | 57 | public function testOffsetMethods() 58 | { 59 | $notification = new Notification(); 60 | $notification->offsetSet('foo', 'bar'); 61 | $this->assertTrue($notification->offsetExists('foo')); 62 | $this->assertSame('bar', $notification->offsetGet('foo')); 63 | $notification->offsetUnset('foo'); 64 | $this->assertFalse($notification->offsetExists('foo')); 65 | } 66 | 67 | public function testGetText() 68 | { 69 | $category = $this->createCategory(); 70 | $from = $this->createUser(); 71 | $to = $this->createUser(); 72 | $notification = new Notification(); 73 | $notification->set('category_id', $category->getKey()); 74 | $notification->set('from_id', $from->getKey()); 75 | $notification->set('to_id', $to->getKey()); 76 | 77 | $this->assertSame('Notification send from #1 to #2.', $notification->getText()); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /tests/integration/Builder/BuilderTest.php: -------------------------------------------------------------------------------- 1 | category(1) 15 | ->from(1) 16 | ->to(2) 17 | ->getNotification(); 18 | 19 | $this->assertInstanceOf(Notification::class, $notification); 20 | 21 | $this->assertSame(1, $notification->category_id); 22 | $this->assertSame(1, $notification->from_id); 23 | $this->assertSame('Fenos\Tests\Models\User', $notification->from_type); 24 | $this->assertSame(2, $notification->to_id); 25 | $this->assertSame('Fenos\Tests\Models\User', $notification->to_type); 26 | $this->assertInstanceOf(Carbon::class, $notification->created_at); 27 | $this->assertInstanceOf(Carbon::class, $notification->updated_at); 28 | } 29 | 30 | public function testCreateSingleAnonymousNotification() 31 | { 32 | $builder = new Builder(); 33 | $notification = $builder 34 | ->category(1) 35 | ->anonymous() 36 | ->to(2) 37 | ->getNotification(); 38 | 39 | $this->assertInstanceOf(Notification::class, $notification); 40 | 41 | $this->assertSame(1, $notification->category_id); 42 | $this->assertNull($notification->from_id); 43 | $this->assertNull($notification->from_type); 44 | $this->assertSame(2, $notification->to_id); 45 | $this->assertSame('Fenos\Tests\Models\User', $notification->to_type); 46 | $this->assertInstanceOf(Carbon::class, $notification->created_at); 47 | $this->assertInstanceOf(Carbon::class, $notification->updated_at); 48 | } 49 | 50 | public function testCreateSingleNotificationWithAll() 51 | { 52 | $builder = new Builder(); 53 | $notification = $builder 54 | ->category(1) 55 | ->from(1) 56 | ->to(2) 57 | ->url('http://notifynder.info') 58 | ->extra([ 59 | 'foo' => 'bar', 60 | ]) 61 | ->expire(Carbon::tomorrow()) 62 | ->getNotification(); 63 | 64 | $this->assertInstanceOf(Notification::class, $notification); 65 | 66 | $this->assertSame('http://notifynder.info', $notification->url); 67 | $this->assertInternalType('array', $notification->extra); 68 | $this->assertCount(1, $notification->extra); 69 | $this->assertSame('bar', $notification->extra['foo']); 70 | $this->assertInstanceOf(Carbon::class, $notification->expires_at); 71 | } 72 | 73 | public function testCreateSingleNotificationWithExtendedExtra() 74 | { 75 | $builder = new Builder(); 76 | $notification = $builder 77 | ->category(1) 78 | ->from(1) 79 | ->to(2) 80 | ->extra([ 81 | 'foo' => 'bar', 82 | ], false) 83 | ->extra([ 84 | 'hello' => 'world', 85 | ], false) 86 | ->getNotification(); 87 | 88 | $this->assertInstanceOf(Notification::class, $notification); 89 | 90 | $this->assertInternalType('array', $notification->extra); 91 | $this->assertCount(2, $notification->extra); 92 | $this->assertSame('bar', $notification->extra['foo']); 93 | $this->assertSame('world', $notification->extra['hello']); 94 | } 95 | 96 | public function testCreateSingleNotificationWithOverriddenExtra() 97 | { 98 | $builder = new Builder(); 99 | $notification = $builder 100 | ->category(1) 101 | ->from(1) 102 | ->to(2) 103 | ->extra([ 104 | 'foo' => 'bar', 105 | ], true) 106 | ->extra([ 107 | 'hello' => 'world', 108 | ], true) 109 | ->getNotification(); 110 | 111 | $this->assertInstanceOf(Notification::class, $notification); 112 | 113 | $this->assertInternalType('array', $notification->extra); 114 | $this->assertCount(1, $notification->extra); 115 | $this->assertSame('world', $notification->extra['hello']); 116 | } 117 | 118 | public function testCreateSingleNotificationAndGetArray() 119 | { 120 | $builder = new Builder(); 121 | $notifications = $builder 122 | ->category(1) 123 | ->from(1) 124 | ->to(2) 125 | ->getNotifications(); 126 | 127 | $this->assertInternalType('array', $notifications); 128 | $this->assertCount(1, $notifications); 129 | 130 | $this->assertInstanceOf(Notification::class, $notifications[0]); 131 | } 132 | 133 | public function testCreateSingleUnvalidNotification() 134 | { 135 | $this->expectException(UnvalidNotificationException::class); 136 | 137 | $builder = new Builder(); 138 | $builder 139 | ->from(1) 140 | ->to(2) 141 | ->getNotification(); 142 | } 143 | 144 | public function testCreateSingleCatchedUnvalidNotificationW() 145 | { 146 | try { 147 | $builder = new Builder(); 148 | $builder 149 | ->from(1) 150 | ->to(2) 151 | ->getNotification(); 152 | } catch (UnvalidNotificationException $e) { 153 | $this->assertInstanceOf(Notification::class, $e->getNotification()); 154 | } 155 | } 156 | 157 | public function testCreateMultipleNotifications() 158 | { 159 | $datas = [2, 3, 4]; 160 | $builder = new Builder(); 161 | $notifications = $builder->loop($datas, function ($builder, $data) { 162 | $builder->category(1) 163 | ->from(1) 164 | ->to($data); 165 | })->getNotifications(); 166 | 167 | $this->assertInternalType('array', $notifications); 168 | $this->assertCount(count($datas), $notifications); 169 | 170 | foreach ($notifications as $index => $notification) { 171 | $this->assertInstanceOf(Notification::class, $notification); 172 | 173 | $this->assertSame(1, $notification->category_id); 174 | $this->assertSame(1, $notification->from_id); 175 | $this->assertSame('Fenos\Tests\Models\User', $notification->from_type); 176 | $this->assertSame($datas[$index], $notification->to_id); 177 | $this->assertSame('Fenos\Tests\Models\User', $notification->to_type); 178 | $this->assertInstanceOf(Carbon::class, $notification->created_at); 179 | $this->assertInstanceOf(Carbon::class, $notification->updated_at); 180 | } 181 | } 182 | 183 | public function testCreateMultipleUnvalidNotifications() 184 | { 185 | $this->expectException(UnvalidNotificationException::class); 186 | 187 | $builder = new Builder(); 188 | $builder->loop([2, 3, 4], function ($builder, $data) { 189 | $builder->category(1) 190 | ->to($data); 191 | })->getNotifications(); 192 | } 193 | 194 | public function testCreateSingleNotificationWithAdditionalField() 195 | { 196 | notifynder_config()->set('additional_fields.fillable', []); 197 | 198 | $builder = new Builder(); 199 | $notification = $builder 200 | ->category(1) 201 | ->from(1) 202 | ->to(2) 203 | ->setField('additional_field', 'value') 204 | ->getNotification(); 205 | 206 | $this->assertInstanceOf(Notification::class, $notification); 207 | $this->assertSame(1, $notification->category_id); 208 | $this->assertNull($notification->additional_field); 209 | 210 | notifynder_config()->set('additional_fields.fillable', ['additional_field']); 211 | 212 | $builder = new Builder(); 213 | $notification = $builder 214 | ->category(1) 215 | ->from(1) 216 | ->to(2) 217 | ->setField('additional_field', 'value') 218 | ->getNotification(); 219 | 220 | $this->assertInstanceOf(Notification::class, $notification); 221 | $this->assertSame(1, $notification->category_id); 222 | $this->assertSame('value', $notification->additional_field); 223 | } 224 | 225 | public function testCreateSingleUnvalidNotificationWithRequiredField() 226 | { 227 | $this->expectException(UnvalidNotificationException::class); 228 | 229 | notifynder_config()->set('additional_fields.required', ['required_field']); 230 | 231 | $builder = new Builder(); 232 | $notification = $builder 233 | ->category(1) 234 | ->from(1) 235 | ->to(2) 236 | ->getNotification(); 237 | } 238 | 239 | public function testCreateSingleNotificationWithRequiredField() 240 | { 241 | notifynder_config()->set('additional_fields.required', ['required_field']); 242 | 243 | $builder = new Builder(); 244 | $notification = $builder 245 | ->category(1) 246 | ->from(1) 247 | ->to(2) 248 | ->setField('required_field', 'value') 249 | ->getNotification(); 250 | 251 | $this->assertInstanceOf(Notification::class, $notification); 252 | $this->assertSame(1, $notification->category_id); 253 | $this->assertSame('value', $notification->required_field); 254 | } 255 | 256 | public function testCreateSingleNotificationWithSplittedEntityData() 257 | { 258 | $builder = new Builder(); 259 | $notification = $builder 260 | ->category(1) 261 | ->from(\Fenos\Tests\Models\User::class, 1) 262 | ->to(\Fenos\Tests\Models\User::class, 2) 263 | ->getNotification(); 264 | 265 | $this->assertInstanceOf(Notification::class, $notification); 266 | $this->assertSame(1, $notification->category_id); 267 | $this->assertSame(1, $notification->from_id); 268 | $this->assertSame(\Fenos\Tests\Models\User::class, $notification->from_type); 269 | $this->assertSame(2, $notification->to_id); 270 | $this->assertSame(\Fenos\Tests\Models\User::class, $notification->to_type); 271 | } 272 | 273 | public function testOffsetMethods() 274 | { 275 | $builder = new Builder(); 276 | $builder->offsetSet('foo', 'bar'); 277 | $this->assertTrue($builder->offsetExists('foo')); 278 | $this->assertSame('bar', $builder->offsetGet('foo')); 279 | $builder->offsetUnset('foo'); 280 | $this->assertFalse($builder->offsetExists('foo')); 281 | } 282 | } 283 | -------------------------------------------------------------------------------- /tests/integration/Collections/ConfigTest.php: -------------------------------------------------------------------------------- 1 | assertInternalType('bool', $config->isPolymorphic()); 11 | } 12 | 13 | public function testIsStrict() 14 | { 15 | $config = app('notifynder.config'); 16 | $this->assertInternalType('bool', $config->isStrict()); 17 | } 18 | 19 | public function testIsTranslated() 20 | { 21 | $config = app('notifynder.config'); 22 | $this->assertInternalType('bool', $config->isTranslated()); 23 | } 24 | 25 | public function testGetNotifiedModel() 26 | { 27 | $config = app('notifynder.config'); 28 | $this->assertInternalType('string', $config->getNotifiedModel()); 29 | $this->assertSame(User::class, $config->getNotifiedModel()); 30 | } 31 | 32 | public function testGetNotifiedModelFail() 33 | { 34 | $this->expectException(InvalidArgumentException::class); 35 | 36 | $config = app('notifynder.config'); 37 | $config->set('model', 'undefined_class_name'); 38 | $config->getNotifiedModel(); 39 | } 40 | 41 | public function testGetAdditionalFields() 42 | { 43 | $config = app('notifynder.config'); 44 | $config->set('additional_fields.fillable', ['fillable_field']); 45 | $config->set('additional_fields.required', ['required_field']); 46 | $this->assertInternalType('array', $config->getAdditionalFields()); 47 | $this->assertCount(2, $config->getAdditionalFields()); 48 | $this->assertSame(['required_field', 'fillable_field'], $config->getAdditionalFields()); 49 | } 50 | 51 | public function testGetAdditionalFieldsFillable() 52 | { 53 | $config = app('notifynder.config'); 54 | $config->set('additional_fields.fillable', ['fillable_field']); 55 | $this->assertInternalType('array', $config->getAdditionalFields()); 56 | $this->assertSame(['fillable_field'], $config->getAdditionalFields()); 57 | } 58 | 59 | public function testGetAdditionalFieldsRequired() 60 | { 61 | $config = app('notifynder.config'); 62 | $config->set('additional_fields.required', ['required_field']); 63 | $this->assertInternalType('array', $config->getAdditionalFields()); 64 | $this->assertSame(['required_field'], $config->getAdditionalFields()); 65 | } 66 | 67 | public function testGetAdditionalFieldsEmpty() 68 | { 69 | $config = app('notifynder.config'); 70 | $this->assertInternalType('array', $config->getAdditionalFields()); 71 | $this->assertSame([], $config->getAdditionalFields()); 72 | } 73 | 74 | public function testGetAdditionalRequiredFields() 75 | { 76 | $config = app('notifynder.config'); 77 | $this->assertInternalType('array', $config->getAdditionalRequiredFields()); 78 | $this->assertSame([], $config->getAdditionalRequiredFields()); 79 | } 80 | 81 | public function testGetTranslationDomain() 82 | { 83 | $config = app('notifynder.config'); 84 | $this->assertInternalType('string', $config->getTranslationDomain()); 85 | $this->assertSame('notifynder', $config->getTranslationDomain()); 86 | } 87 | 88 | public function testHasTrue() 89 | { 90 | $config = app('notifynder.config'); 91 | $this->assertTrue($config->has('polymorphic')); 92 | } 93 | 94 | public function testHasFalse() 95 | { 96 | $config = app('notifynder.config'); 97 | $this->assertFalse($config->has('undefined_config_key')); 98 | } 99 | 100 | public function testSet() 101 | { 102 | $config = app('notifynder.config'); 103 | $config->set('polymorphic', true); 104 | $this->assertTrue($config->get('polymorphic')); 105 | } 106 | 107 | public function testGetOverloaded() 108 | { 109 | $config = app('notifynder.config'); 110 | $this->assertInternalType('bool', $config->polymorphic); 111 | } 112 | 113 | public function testSetOverloaded() 114 | { 115 | $config = app('notifynder.config'); 116 | 117 | $config->polymorphic = true; 118 | $this->assertInternalType('bool', $config->polymorphic); 119 | $this->assertTrue($config->get('polymorphic')); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /tests/integration/Facades/NotifynderFacadeTest.php: -------------------------------------------------------------------------------- 1 | createCategory(); 11 | $sent = \Notifynder::category($category->getKey()) 12 | ->from(1) 13 | ->to(2) 14 | ->send(); 15 | 16 | $this->assertTrue($sent); 17 | 18 | $notifications = ModelNotification::all(); 19 | $this->assertCount(1, $notifications); 20 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/integration/Helpers/HelpersTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(Config::class, notifynder_config()); 10 | } 11 | 12 | public function testNotifynderConfigGet() 13 | { 14 | $this->assertInternalType('bool', notifynder_config('polymorphic')); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/integration/Helpers/TypeCheckerTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(TypeChecker::isString('hello world')); 11 | } 12 | 13 | public function testIsStringFailStrict() 14 | { 15 | $this->expectException(InvalidArgumentException::class); 16 | TypeChecker::isString(15); 17 | } 18 | 19 | public function testIsStringFail() 20 | { 21 | $this->assertFalse(TypeChecker::isString(15, false)); 22 | } 23 | 24 | public function testIsNumeric() 25 | { 26 | $this->assertTrue(TypeChecker::isNumeric(15)); 27 | } 28 | 29 | public function testIsNumericFailStrict() 30 | { 31 | $this->expectException(InvalidArgumentException::class); 32 | TypeChecker::isNumeric('hello world'); 33 | } 34 | 35 | public function testIsNumericFail() 36 | { 37 | $this->assertFalse(TypeChecker::isNumeric('hello world', false)); 38 | } 39 | 40 | public function testIsDate() 41 | { 42 | $this->assertTrue(TypeChecker::isDate(Carbon::now())); 43 | } 44 | 45 | public function testIsDateFailStrict() 46 | { 47 | $this->expectException(InvalidArgumentException::class); 48 | TypeChecker::isDate('hello world'); 49 | } 50 | 51 | public function testIsDateFail() 52 | { 53 | $this->assertFalse(TypeChecker::isDate('hello world', false)); 54 | } 55 | 56 | public function testIsArray() 57 | { 58 | $this->assertTrue(TypeChecker::isArray([1, 2, 3])); 59 | } 60 | 61 | public function testIsArrayFailStrict() 62 | { 63 | $this->expectException(InvalidArgumentException::class); 64 | TypeChecker::isArray(collect([1, 2, 3])); 65 | } 66 | 67 | public function testIsArrayFail() 68 | { 69 | $this->assertFalse(TypeChecker::isArray(collect([1, 2, 3]), false)); 70 | } 71 | 72 | public function testIsIterable() 73 | { 74 | $this->assertTrue(TypeChecker::isIterable(collect([1, 2, 3]))); 75 | } 76 | 77 | public function testIsIterableFailStrict() 78 | { 79 | $this->expectException(InvalidArgumentException::class); 80 | TypeChecker::isIterable([]); 81 | } 82 | 83 | public function testIsIterableFail() 84 | { 85 | $this->assertFalse(TypeChecker::isIterable([], false)); 86 | } 87 | 88 | public function testIsNotification() 89 | { 90 | $this->assertTrue(TypeChecker::isNotification(new \Fenos\Notifynder\Models\Notification())); 91 | } 92 | 93 | public function testIsNotificationFailStrict() 94 | { 95 | $this->expectException(InvalidArgumentException::class); 96 | TypeChecker::isNotification([]); 97 | } 98 | 99 | public function testIsNotificationFail() 100 | { 101 | $this->assertFalse(TypeChecker::isNotification([], false)); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/integration/Managers/NotifynderManagerTest.php: -------------------------------------------------------------------------------- 1 | expectException(BadMethodCallException::class); 15 | 16 | $manager = app('notifynder'); 17 | $manager->undefinedMethod(); 18 | } 19 | 20 | public function testGetBuilderInstance() 21 | { 22 | $manager = app('notifynder'); 23 | $builder = $manager->builder(); 24 | 25 | $this->assertInstanceOf(Builder::class, $builder); 26 | } 27 | 28 | public function testGetSenderInstance() 29 | { 30 | $manager = app('notifynder'); 31 | $sender = $manager->sender(); 32 | 33 | $this->assertInstanceOf(SenderManager::class, $sender); 34 | } 35 | 36 | public function testBuildSingleNotification() 37 | { 38 | $manager = app('notifynder'); 39 | $notification = $manager->category(1) 40 | ->from(1) 41 | ->to(2) 42 | ->getNotification(); 43 | 44 | $this->assertInstanceOf(BuilderNotification::class, $notification); 45 | } 46 | 47 | public function testBuildMultipleNotifications() 48 | { 49 | $datas = [2, 3, 4]; 50 | $manager = app('notifynder'); 51 | $notifications = $manager->loop($datas, function ($builder, $data) { 52 | $builder->category(1) 53 | ->from(1) 54 | ->to($data); 55 | })->getNotifications(); 56 | 57 | $this->assertInternalType('array', $notifications); 58 | $this->assertCount(count($datas), $notifications); 59 | } 60 | 61 | public function testSendSingleNotification() 62 | { 63 | $manager = app('notifynder'); 64 | $category = $this->createCategory(); 65 | $sent = $manager->category($category->getKey()) 66 | ->from(1) 67 | ->to(2) 68 | ->send(); 69 | 70 | $this->assertTrue($sent); 71 | 72 | $notifications = ModelNotification::all(); 73 | $this->assertCount(1, $notifications); 74 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 75 | } 76 | 77 | public function testSendSingleAnonymousNotification() 78 | { 79 | $manager = app('notifynder'); 80 | $category = $this->createCategory(); 81 | $sent = $manager->category($category->getKey()) 82 | ->anonymous() 83 | ->to(2) 84 | ->send(); 85 | 86 | $this->assertTrue($sent); 87 | 88 | $notifications = ModelNotification::all(); 89 | $this->assertCount(1, $notifications); 90 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 91 | $notification = $notifications->first(); 92 | $this->assertInstanceOf(ModelNotification::class, $notification); 93 | $this->assertNull($notification->from); 94 | $this->assertNull($notification->from_id); 95 | $this->assertNull($notification->from_type); 96 | $this->assertTrue($notification->isAnonymous()); 97 | } 98 | 99 | public function testSendMultipleNotifications() 100 | { 101 | $datas = [2, 3, 4]; 102 | $manager = app('notifynder'); 103 | $category = $this->createCategory(); 104 | $sent = $manager->loop($datas, function ($builder, $data) use ($category) { 105 | $builder->category($category->getKey()) 106 | ->from(1) 107 | ->to($data); 108 | })->send(); 109 | 110 | $this->assertTrue($sent); 111 | 112 | $notifications = ModelNotification::all(); 113 | $this->assertCount(count($datas), $notifications); 114 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 115 | } 116 | 117 | public function testSendSingleSpecificNotification() 118 | { 119 | $manager = app('notifynder'); 120 | $category = $this->createCategory(); 121 | $sent = $manager->category($category->getKey()) 122 | ->from(1) 123 | ->to(2) 124 | ->sendSingle(); 125 | 126 | $this->assertTrue($sent); 127 | 128 | $notifications = ModelNotification::all(); 129 | $this->assertCount(1, $notifications); 130 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 131 | } 132 | 133 | public function testSendOnceSameNotifications() 134 | { 135 | $manager = app('notifynder'); 136 | $category = $this->createCategory(); 137 | $sent = $manager->category($category->getKey()) 138 | ->from(1) 139 | ->to(2) 140 | ->extra(['foo' => 'bar']) 141 | ->sendOnce(); 142 | $this->assertTrue($sent); 143 | 144 | $notifications = ModelNotification::all(); 145 | $this->assertCount(1, $notifications); 146 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 147 | $notificationFirst = $notifications->first(); 148 | $this->assertInstanceOf(Notification::class, $notificationFirst); 149 | 150 | $this->assertEquals(0, $notificationFirst->read); 151 | $notificationFirst->read(); 152 | $this->assertEquals(1, $notificationFirst->read); 153 | 154 | sleep(1); 155 | 156 | $sent = $manager->category($category->getKey()) 157 | ->from(1) 158 | ->to(2) 159 | ->extra(['foo' => 'bar']) 160 | ->sendOnce(); 161 | $this->assertTrue($sent); 162 | 163 | $notifications = ModelNotification::all(); 164 | $this->assertCount(1, $notifications); 165 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 166 | $notificationSecond = $notifications->first(); 167 | $this->assertInstanceOf(Notification::class, $notificationSecond); 168 | 169 | $this->assertEquals(0, $notificationSecond->read); 170 | 171 | $this->assertSame($notificationFirst->getKey(), $notificationSecond->getKey()); 172 | $this->assertEquals($notificationFirst->created_at, $notificationSecond->created_at); 173 | $diff = $notificationFirst->updated_at->diffInSeconds($notificationSecond->updated_at); 174 | $this->assertGreaterThan(0, $diff); 175 | } 176 | 177 | public function testSendOnceDifferentNotifications() 178 | { 179 | $manager = app('notifynder'); 180 | $category = $this->createCategory(); 181 | $sent = $manager->category($category->getKey()) 182 | ->from(1) 183 | ->to(2) 184 | ->extra(['foo' => 'bar']) 185 | ->sendOnce(); 186 | $this->assertTrue($sent); 187 | 188 | $notifications = ModelNotification::all(); 189 | $this->assertCount(1, $notifications); 190 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 191 | 192 | $sent = $manager->category($category->getKey()) 193 | ->from(2) 194 | ->to(1) 195 | ->extra(['hello' => 'world']) 196 | ->sendOnce(); 197 | $this->assertTrue($sent); 198 | 199 | $notifications = ModelNotification::all(); 200 | $this->assertCount(2, $notifications); 201 | $this->assertInstanceOf(EloquentCollection::class, $notifications); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /tests/integration/Managers/SenderManagerTest.php: -------------------------------------------------------------------------------- 1 | expectException(BadMethodCallException::class); 8 | 9 | $manager = app('notifynder.sender'); 10 | $manager->sendSingle(); 11 | } 12 | 13 | public function testCallUndefinedMethod() 14 | { 15 | $this->expectException(BadMethodCallException::class); 16 | 17 | $manager = app('notifynder.sender'); 18 | $manager->undefinedMethod([]); 19 | } 20 | 21 | public function testCallFailingSender() 22 | { 23 | $this->expectException(BadFunctionCallException::class); 24 | 25 | $manager = app('notifynder.sender'); 26 | $manager->extend('sendFail', function () { 27 | }); 28 | $manager->sendFail([]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/integration/Models/NotificationTest.php: -------------------------------------------------------------------------------- 1 | assertInternalType('array', $notification->getFillable()); 11 | $this->assertSame([ 12 | 'to_id', 13 | 'to_type', 14 | 'from_id', 15 | 'from_type', 16 | 'category_id', 17 | 'read', 18 | 'url', 19 | 'extra', 20 | 'expires_at', 21 | 'stack_id', 22 | ], $notification->getFillable()); 23 | } 24 | 25 | public function testFillablesCustomFillables() 26 | { 27 | $config = app('notifynder.config'); 28 | $config->set('additional_fields.fillable', ['fillable_field']); 29 | $notification = new Notification(); 30 | $this->assertInternalType('array', $notification->getFillable()); 31 | $this->assertSame([ 32 | 'to_id', 33 | 'to_type', 34 | 'from_id', 35 | 'from_type', 36 | 'category_id', 37 | 'read', 38 | 'url', 39 | 'extra', 40 | 'expires_at', 41 | 'stack_id', 42 | 'fillable_field', 43 | ], $notification->getFillable()); 44 | } 45 | 46 | public function testFillablesCustomRequired() 47 | { 48 | $config = app('notifynder.config'); 49 | $config->set('additional_fields.required', ['required_field']); 50 | $notification = new Notification(); 51 | $this->assertInternalType('array', $notification->getFillable()); 52 | $this->assertSame([ 53 | 'to_id', 54 | 'to_type', 55 | 'from_id', 56 | 'from_type', 57 | 'category_id', 58 | 'read', 59 | 'url', 60 | 'extra', 61 | 'expires_at', 62 | 'stack_id', 63 | 'required_field', 64 | ], $notification->getFillable()); 65 | } 66 | 67 | public function testFillablesCustom() 68 | { 69 | $config = app('notifynder.config'); 70 | $config->set('additional_fields.fillable', ['fillable_field']); 71 | $config->set('additional_fields.required', ['required_field']); 72 | $notification = new Notification(); 73 | $this->assertInternalType('array', $notification->getFillable()); 74 | $this->assertSame([ 75 | 'to_id', 76 | 'to_type', 77 | 'from_id', 78 | 'from_type', 79 | 'category_id', 80 | 'read', 81 | 'url', 82 | 'extra', 83 | 'expires_at', 84 | 'stack_id', 85 | 'required_field', 86 | 'fillable_field', 87 | ], $notification->getFillable()); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /tests/integration/Parsers/NotificationParserTest.php: -------------------------------------------------------------------------------- 1 | createUser(); 12 | $to = $this->createUser(); 13 | $notification = new Notification(); 14 | $notification->set('category_id', null); 15 | $notification->set('from_id', $from->getKey()); 16 | $notification->set('to_id', $to->getKey()); 17 | 18 | $this->expectException(ModelNotFoundException::class); 19 | $parser = new NotificationParser(); 20 | $parser->parse($notification); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/integration/Resolvers/ModelResolverTest.php: -------------------------------------------------------------------------------- 1 | getModel(Notification::class); 11 | $this->assertEquals(Notification::class, $class); 12 | } 13 | 14 | public function testGetModelCustom() 15 | { 16 | $resolver = app('notifynder.resolver.model'); 17 | $resolver->setModel(Notification::class, 'This\Model\Is\Custom'); 18 | $class = $resolver->getModel(Notification::class); 19 | $this->assertEquals('This\Model\Is\Custom', $class); 20 | } 21 | 22 | public function testGetTableDefault() 23 | { 24 | $resolver = app('notifynder.resolver.model'); 25 | $table = $resolver->getTable(Notification::class); 26 | $this->assertEquals('notifications', $table); 27 | } 28 | 29 | public function testGetTableCustom() 30 | { 31 | $resolver = app('notifynder.resolver.model'); 32 | $resolver->setTable(Notification::class, 'prefixed_notifications'); 33 | $table = $resolver->getTable(Notification::class); 34 | $this->assertEquals('prefixed_notifications', $table); 35 | } 36 | 37 | public function testMakeModel() 38 | { 39 | $resolver = app('notifynder.resolver.model'); 40 | $model = $resolver->make(Notification::class); 41 | $this->assertInstanceOf(Notification::class, $model); 42 | } 43 | 44 | public function testMakeModelFail() 45 | { 46 | $this->expectException(ReflectionException::class); 47 | 48 | $resolver = app('notifynder.resolver.model'); 49 | $resolver->setModel(Notification::class, 'This\Model\Is\Custom'); 50 | $resolver->make(Notification::class); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/integration/Senders/OnceSenderTest.php: -------------------------------------------------------------------------------- 1 | setModel(\Fenos\Notifynder\Models\Notification::class, \Fenos\Tests\Models\FakeModel::class); 8 | 9 | $this->expectException(BadMethodCallException::class); 10 | 11 | $manager = app('notifynder.sender'); 12 | $manager->sendOnce([ 13 | new \Fenos\Notifynder\Builder\Notification(), 14 | ]); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/integration/Traits/NotifableEagerLoadingTest.php: -------------------------------------------------------------------------------- 1 | createUser(); 8 | $this->sendNotificationTo($user); 9 | 10 | $notification = $user->getLazyNotificationRelation()->first(); 11 | $this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); 12 | } 13 | 14 | public function testGetNotificationRelationReadsConfigurationParameterIfNothingIsPassed() 15 | { 16 | $user = $this->createUser(); 17 | $this->sendNotificationTo($user); 18 | 19 | $notification = $user->getNotificationRelation()->first(); 20 | $this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); 21 | } 22 | 23 | public function testGetNotificationRelationCanEagerLoadAllRelationsIfTrueIsPassed() 24 | { 25 | $user = $this->createUser(); 26 | $this->sendNotificationTo($user); 27 | 28 | $notification = $user->getNotificationRelation(true)->first(); 29 | 30 | $this->assertModelHasLoadedRelations($notification, ['category', 'from', 'to']); 31 | } 32 | 33 | public function testGetNotificationRelationCanEagerLoadASubsetOfRelationsIfAnArrayIsPassed() 34 | { 35 | $user = $this->createUser(); 36 | $this->sendNotificationTo($user); 37 | 38 | $notification = $user->getNotificationRelation(['category'])->first(); 39 | 40 | $this->assertModelHasLoadedRelations($notification, ['category']); 41 | $this->assertModelHasNoLoadedRelations($notification, ['from', 'to']); 42 | } 43 | 44 | public function testGetNotificationRelationDoesnotEagerLoadIfConfigurationParameterIsMissing() 45 | { 46 | $user = $this->createUser(); 47 | $this->sendNotificationTo($user); 48 | 49 | $config = app('notifynder.config'); 50 | $config->forget('eager_load'); 51 | 52 | $notification = $user->getNotificationRelation()->first(); 53 | $this->assertModelHasNoLoadedRelations($notification, ['category', 'from', 'to']); 54 | } 55 | 56 | public function testGetNotificationsReadsEagerLoadConfigurationParameter() 57 | { 58 | $user = $this->createUser(); 59 | $this->sendNotificationTo($user); 60 | 61 | $config = app('notifynder.config'); 62 | 63 | $config->set('eager_load', true); 64 | $notifications = $user->getNotifications(); 65 | $this->assertModelHasLoadedRelations($notifications[0], ['category', 'from', 'to']); 66 | 67 | $config->set('eager_load', false); 68 | $notifications = $user->getNotifications(); 69 | $this->assertModelHasNoLoadedRelations($notifications[0], ['category', 'from', 'to']); 70 | 71 | $config->set('eager_load', ['category', 'from']); 72 | $notifications = $user->getNotifications(); 73 | $this->assertModelHasLoadedRelations($notifications[0], ['category', 'from']); 74 | $this->assertModelHasNoLoadedRelations($notifications[0], ['to']); 75 | } 76 | 77 | protected function assertModelHasLoadedRelations($model, $relationNames = []) 78 | { 79 | $modelLoadedRelations = $model->getRelations(); 80 | foreach ($relationNames as $relationName) { 81 | $this->assertArrayHasKey($relationName, $modelLoadedRelations, $relationName.' relation was not eager loaded'); 82 | } 83 | } 84 | 85 | protected function assertModelHasNoLoadedRelations($model, $relationNames = []) 86 | { 87 | $modelLoadedRelations = $model->getRelations(); 88 | foreach ($relationNames as $relationName) { 89 | $this->assertArrayNotHasKey($relationName, $modelLoadedRelations, $relationName.' relation was eager loaded'); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tests/integration/Traits/NotifableTest.php: -------------------------------------------------------------------------------- 1 | createUser(); 12 | $notifynder = $user->notifynder(1); 13 | $this->assertInstanceOf(NotifynderManager::class, $notifynder); 14 | $notifynder->from(1)->to(2); 15 | $builder = $notifynder->builder(); 16 | $this->assertInstanceOf(Builder::class, $builder); 17 | $notification = $builder->getNotification(); 18 | $this->assertInstanceOf(Notification::class, $notification); 19 | $this->assertSame(1, $notification->category_id); 20 | } 21 | 22 | public function testSendNotificationFrom() 23 | { 24 | $category = $this->createCategory(); 25 | $user = $this->createUser(); 26 | $notifynder = $user->sendNotificationFrom($category->getKey()); 27 | $this->assertInstanceOf(NotifynderManager::class, $notifynder); 28 | $notifynder->to(2); 29 | $builder = $notifynder->builder(); 30 | $this->assertInstanceOf(Builder::class, $builder); 31 | $notification = $builder->getNotification(); 32 | $this->assertInstanceOf(Notification::class, $notification); 33 | $this->assertSame($category->getKey(), $notification->category_id); 34 | $this->assertSame($user->getKey(), $notification->from_id); 35 | } 36 | 37 | public function testSendNotificationTo() 38 | { 39 | $category = $this->createCategory(); 40 | $user = $this->createUser(); 41 | $notifynder = $user->sendNotificationTo($category->getKey()); 42 | $this->assertInstanceOf(NotifynderManager::class, $notifynder); 43 | $notifynder->from(2); 44 | $builder = $notifynder->builder(); 45 | $this->assertInstanceOf(Builder::class, $builder); 46 | $notification = $builder->getNotification(); 47 | $this->assertInstanceOf(Notification::class, $notification); 48 | $this->assertSame($category->getKey(), $notification->category_id); 49 | $this->assertSame($user->getKey(), $notification->to_id); 50 | $notifynder->send(); 51 | $this->assertCount(1, $user->getNotificationRelation); 52 | } 53 | 54 | public function testNotificationsHasMany() 55 | { 56 | $category = $this->createCategory(); 57 | $user = $this->createUser(); 58 | $user 59 | ->sendNotificationTo($category->getKey()) 60 | ->from(2) 61 | ->send(); 62 | $this->assertCount(1, $user->getNotificationRelation); 63 | } 64 | 65 | public function testNotificationsMorphMany() 66 | { 67 | notifynder_config()->set('polymorphic', true); 68 | 69 | $user = $this->createUser(); 70 | $this->sendNotificationTo($user); 71 | $car = $this->createCar(); 72 | $this->sendNotificationTo($car); 73 | $this->assertCount(1, $user->getNotificationRelation); 74 | $this->assertCount(1, $car->getNotificationRelation); 75 | } 76 | 77 | public function testGetNotificationsDefault() 78 | { 79 | $user = $this->createUser(); 80 | $this->sendNotificationsTo($user, 25); 81 | $this->assertCount(25, $user->getNotifications()); 82 | } 83 | 84 | public function testGetNotificationsLimited() 85 | { 86 | $user = $this->createUser(); 87 | $this->sendNotificationsTo($user, 25); 88 | $this->assertCount(10, $user->getNotifications(10)); 89 | } 90 | 91 | public function testGetNotificationsNotRead() 92 | { 93 | $user = $this->createUser(); 94 | $this->sendNotificationsTo($user, 25); 95 | $this->assertSame(25, $user->readAllNotifications()); 96 | $this->assertCount(0, $user->getNotificationsNotRead()); 97 | } 98 | 99 | public function testGetNotificationsNotReadLimited() 100 | { 101 | $user = $this->createUser(); 102 | $this->sendNotificationsTo($user, 25); 103 | $this->assertCount(10, $user->getNotificationsNotRead(10)); 104 | } 105 | 106 | public function testReadStatusRelatedMethods() 107 | { 108 | $user = $this->createUser(); 109 | $this->sendNotificationsTo($user, 25); 110 | $this->assertSame(25, $user->countUnreadNotifications()); 111 | $this->assertSame(25, $user->readAllNotifications()); 112 | $this->assertSame(0, $user->countUnreadNotifications()); 113 | $this->assertSame(25, $user->unreadAllNotifications()); 114 | $this->assertSame(25, $user->countUnreadNotifications()); 115 | $notification = $user->getNotificationRelation->first(); 116 | $this->assertTrue($user->readNotification($notification)); 117 | $this->assertSame(24, $user->countUnreadNotifications()); 118 | $this->assertTrue($user->unreadNotification($notification->getKey())); 119 | $this->assertSame(25, $user->countUnreadNotifications()); 120 | 121 | $user2 = $this->createUser(); 122 | $this->sendNotificationsTo($user2, 5); 123 | $this->assertFalse($user->readNotification($user2->getNotificationRelation->first())); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /tests/migrations/2014_08_01_164248_create_users_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('firstname'); 18 | $table->string('lastname'); 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::drop('users'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/migrations/2016_08_26_100534_create_cars_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('brand'); 18 | $table->string('model'); 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::drop('cars'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/models/Car.php: -------------------------------------------------------------------------------- 1 |