<commit_before># Command Bus

- [Introduction](#introduction)
- [Creating Commamnds](#creating-commands)
- [Dispatching Commamnds](#dispatching-commands)
- [Queued Commands](#queued-commands)

<a name="introduction"></a>
## Introduction

The Laravel command bus provides a convenient method of encapsulating tasks your application needs to perform into simple, easy to understand "commands". To help us understand the purpose of commands, let's pretend we are building an application that allows users to purchase podcasts.

When a user purchases a podcast, there are a variety of things that need to happen. For example, we may need to charge the user's credit card, add a record to our database that represents the purchase, and send a confirmation e-mail of the purchase. Perhaps we also need to perform some kind of validation as to whether the user is allowed to purchase podcasts.

We could put all of this logic inside a controller method; however, this has several disadvantages. The first disadvantage is that our controller probably handles several other incoming HTTP actions, and including complicated logic in each controller method will soon bloat our controller and make it harder to read. Secondly, it is difficult to re-use the purchase podcast logic outside of the controller context. Thirdly, it is more difficult to unit-test the command as we must also generate a stub HTTP request and make a full request to the application to test the purchase podcast logic.

Instead of putting this logic in the controller, we may choose to encapsulte it within a "command" object, such as `PurchasePodcast` command.

<a name="creating-commands"></a>
## Creating Commands

The Artisan CLI can generate new command classes using the `make:command` command:

	php artisan make:command PurchasePodcast

The newly generated class will be placed in the `app/Commands` directory. By default, the command contains two methods: the constructor and the `handle` method. Of course, the constructor allows you to pass any relevant objects to the command, while the `handle` method executes the command. For example:

	class PurchasePodcast extends Command implements SelfHandling {

		protected $user, $podcast;

		/**<commit_msg>Letter.<commit_after># Command Bus

- [Introduction](#introduction)
- [Creating Commamnds](#creating-commands)
- [Dispatching Commamnds](#dispatching-commands)
- [Queued Commands](#queued-commands)

<a name="introduction"></a>
## Introduction

The Laravel command bus provides a convenient method of encapsulating tasks your application needs to perform into simple, easy to understand "commands". To help us understand the purpose of commands, let's pretend we are building an application that allows users to purchase podcasts.

When a user purchases a podcast, there are a variety of things that need to happen. For example, we may need to charge the user's credit card, add a record to our database that represents the purchase, and send a confirmation e-mail of the purchase. Perhaps we also need to perform some kind of validation as to whether the user is allowed to purchase podcasts.

We could put all of this logic inside a controller method; however, this has several disadvantages. The first disadvantage is that our controller probably handles several other incoming HTTP actions, and including complicated logic in each controller method will soon bloat our controller and make it harder to read. Secondly, it is difficult to re-use the purchase podcast logic outside of the controller context. Thirdly, it is more difficult to unit-test the command as we must also generate a stub HTTP request and make a full request to the application to test the purchase podcast logic.

Instead of putting this logic in the controller, we may choose to encapsulte it within a "command" object, such as a `PurchasePodcast` command.

<a name="creating-commands"></a>
## Creating Commands

The Artisan CLI can generate new command classes using the `make:command` command:

	php artisan make:command PurchasePodcast

The newly generated class will be placed in the `app/Commands` directory. By default, the command contains two methods: the constructor and the `handle` method. Of course, the constructor allows you to pass any relevant objects to the command, while the `handle` method executes the command. For example:

	class PurchasePodcast extends Command implements SelfHandling {

		protected $user, $podcast;

		/**
{'fraction_non_alphanumeric': 0.05146067415730337, 'fraction_numerical': 0.0, 'mean_word_length': 5.006747638326586, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2284079', 'n_tokens_mistral': 1047, 'n_tokens_neox': 995, 'n_words': 631}