### Environment Variable Setup
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/README.md
Instructions for setting up environment variables by copying the example .env file and adding your API key.
```bash
cp .env_example .env
```
--------------------------------
### Quick Start GET Request
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/README.md
Example of making a GET request to a RESTful API using the SendGrid HTTP Client. Ensure you replace YOUR_SENDGRID_API_KEY and adjust the endpoint and parameters as needed.
```php
// include __DIR__ . '/loader.php';
require 'vendor/autoload.php';
$apiKey = YOUR_SENDGRID_API_KEY;
$authHeaders = [
'Authorization: Bearer ' . $apiKey
];
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
$param = 'foo';
$response = $client->your()->api()->_($param)->call()->get();
var_dump(
$response->statusCode(),
$response->headers(),
$response->body()
);
```
--------------------------------
### Quick Start POST Request
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/README.md
Example of making a POST request with headers, query parameters, and a request body. This demonstrates versioning and custom headers. Replace placeholders and adjust as necessary.
```php
// include __DIR__ . '/loader.php';
require 'vendor/autoload.php';
$apiKey = YOUR_SENDGRID_API_KEY;
$authHeaders = [
'Authorization: Bearer ' . $apiKey
];
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
$queryParams = [
'hello' => 0, 'world' => 1
];
$requestHeaders = [
'X-Test' => 'test'
];
$data = [
'some' => 1, 'awesome' => 2, 'data' => 3
];
$param = 'bar';
$response = $client->your()->api()->_($param)->call()->post($data, $queryParams, $requestHeaders);
var_dump(
$response->statusCode(),
$response->headers(),
$response->body()
);
```
--------------------------------
### Mock vs Spy Example
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/mockery/mockery/docs/reference/spies.rst
Illustrates the difference in setup between using a mock with pre-defined expectations and a spy that asserts calls after the action.
```php
// arrange
$mock = \Mockery::mock('MyDependency');
$sut = new MyClass($mock);
// expect
$mock->shouldReceive('foo')
->once()
->with('bar');
// act
$sut->callFoo();
// assert
\Mockery::close();
```
```php
// arrange
$spy = \Mockery::spy('MyDependency');
$sut = new MyClass($spy);
// act
$sut->callFoo();
// assert
$spy->shouldHaveReceived()
->foo()
->with('bar');
```
--------------------------------
### Install PHP-HTTP Client Common
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/php-http/client-common/README.md
Install the client-common package using Composer.
```bash
composer require php-http/client-common
```
--------------------------------
### Composer Installation
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/README.md
Add the php-http-client to your project's composer.json file and run composer install.
```json
{
"require": {
"sendgrid/php-http-client": "^4.1.1"
}
}
```
--------------------------------
### Manual Installation Client Instantiation
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/README.md
Instantiate the SendGrid
Client after including the loader.php file for manual installation.
```php
= 5.6
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/ralouphie/getallheaders/README.md
Use this command to install the library if your PHP version is 5.6 or higher.
```bash
composer require ralouphie/getallheaders
```
--------------------------------
### Install php-http/message-factory via Composer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/php-http/message-factory/README.md
Use Composer to install the php-http/message-factory package.
```bash
$ composer require php-http/message-factory
```
--------------------------------
### Clone Repository and Install Dependencies
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/CONTRIBUTING.md
Clone the sendgrid-php repository and install its dependencies using Composer.
```bash
git clone https://github.com/sendgrid/sendgrid-php.git
cd sendgrid-php
composer install
```
--------------------------------
### Install sebastian/comparator with Composer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/comparator/README.md
Install the comparator library as a local project dependency using Composer.
```bash
composer require sebastian/comparator
```
--------------------------------
### Install sebastian/exporter with Composer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/exporter/README.md
Install the library as a local, per-project dependency using Composer.
```bash
composer require sebastian/exporter
```
--------------------------------
### Install sebastian/object-enumerator with Composer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/object-enumerator/README.md
Install the library as a local, per-project dependency using Composer.
```bash
composer require sebastian/object-enumerator
```
--------------------------------
### Install Mockery via Composer Command Line
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/mockery/mockery/docs/getting_started/installation.rst
A direct command-line method to install Mockery as a development dependency. This is an alternative to manually editing composer.json.
```bash
php composer.phar require --dev mockery/mockery
```
--------------------------------
### Install PHP HTTP Message via Composer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/php-http/message/README.md
Install the package using Composer by running the command in your terminal.
```bash
$ composer require php-http/message
```
--------------------------------
### Install Twilio SDK for PHP
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/USE_CASES.md
Install the Twilio SDK for PHP using Composer. This is required before sending SMS messages.
```bash
composer require twilio/sdk
```
--------------------------------
### Install sebastian/comparator as a Development Dependency
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/comparator/README.md
Install the comparator library as a development-time dependency for tasks like running test suites.
```bash
composer require --dev sebastian/comparator
```
--------------------------------
### Send Email with PHPMailer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/PHPMailer/README.md
This example demonstrates how to send an email using PHPMailer with SMTP authentication. It includes server configuration, recipient setup, attachments, and HTML/plain text content. Ensure you have Composer installed and have required the autoloader.
```php
SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.example.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'user@example.com'; //SMTP username
$mail->Password = 'secret'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); //Add a recipient
$mail->addAddress('ellen@example.com'); //Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body in bold!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
```
--------------------------------
### GET Collection Data
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/USAGE.md
Example of making a GET request to retrieve a collection of resources, including query parameters and mock response headers.
```php
$query_params = ['limit' => 100, 'offset' => 0];
$request_headers = ['X-Mock: 200'];
$response = $client->api_keys()->get(null, $query_params, $request_headers);
echo $response->statusCode();
echo $response->body();
echo $response->headers();
```
--------------------------------
### Install sebastian/recursion-context as a development dependency
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/recursion-context/README.md
Use this command to add the library as a development-time dependency, for example, for running tests.
```bash
composer require --dev sebastian/recursion-context
```
--------------------------------
### Install sebastian/object-reflector as a Development Dependency
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/object-reflector/README.md
Use this command to add the library as a development-time dependency, for example, for running test suites.
```bash
composer require --dev sebastian/object-reflector
```
--------------------------------
### Install sebastian/complexity as a Development Dependency
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/sebastian/complexity/README.md
Use this command to add the library as a development-time dependency, for example, for running test suites.
```bash
composer require --dev sebastian/complexity
```
--------------------------------
### Get PHPUnit Version from PHAR
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/phpunit/phpunit/README.md
Check the installed PHPUnit version using the PHAR archive. Replace X.Y with the downloaded version.
```bash
$ php phpunit-X.Y.phar --version
```
--------------------------------
### Manual Installation Loader
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/README.md
Create a loader.php file to include the necessary client and response classes when not using Composer.
```php
client->suppression()->bounces()->get();
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}
```
--------------------------------
### Get Bounces using SendGrid v3 Web API (Without Fluent Interface)
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/README.md
This example demonstrates retrieving bounce data using the SendGrid v3 Web API without the fluent interface. The SENDGRID_API_KEY environment variable must be configured.
```php
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
try {
$response = $sg->client->_("suppression/bounces")->get();
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}
```
--------------------------------
### Example Fetcher Class with Instance Method
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/mockery/mockery/docs/cookbook/class_constants.rst
A sample class with an instance method and class constants, for demonstrating non-static mocking.
```php
class Fetcher
{
const SUCCESS = 0;
const FAILURE = 1;
public function fetch()
{
// Fetcher gets something for us from somewhere...
return self::SUCCESS;
}
}
class MyClass
{
public function doFetching($fetcher)
{
$response = $fetcher->fetch();
if ($response == Fetcher::SUCCESS) {
echo "Thanks!" . PHP_EOL;
} else {
echo "Try again!" . PHP_EOL;
}
}
}
```
--------------------------------
### Example Fetcher Class
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/mockery/mockery/docs/cookbook/class_constants.rst
A sample class with static method and class constants.
```php
class Fetcher
{
const SUCCESS = 0;
const FAILURE = 1;
public static function fetch()
{
// Fetcher gets something for us from somewhere...
return self::SUCCESS;
}
}
class MyClass
{
public function doFetching()
{
$response = Fetcher::fetch();
if ($response == Fetcher::SUCCESS) {
echo "Thanks!" . PHP_EOL;
} else {
echo "Try again!" . PHP_EOL;
}
}
}
```
--------------------------------
### Install Nyholm/psr7
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/nyholm/psr7/README.md
Use Composer to install the Nyholm/psr7 package.
```bash
composer require nyholm/psr7
```
--------------------------------
### Install GuzzleHttp PSR-7
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/guzzlehttp/psr7/README.md
Use Composer to install the guzzlehttp/psr7 package.
```shell
composer require guzzlehttp/psr7
```
--------------------------------
### Initialize SendGrid Client
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/USAGE.md
Demonstrates how to initialize the SendGrid client with an API key and base URL. Ensure you have the necessary autoloader or client file included.
```php
// If running this outside of this context, use the following include and
// comment out the two includes below
// require __DIR__ . '/vendor/autoload.php';
include(dirname(__DIR__) . '/lib/Client.php');
// This gets the parent directory, for your current directory use getcwd()
$path_to_config = dirname(__DIR__);
$apiKey = getenv('SENDGRID_API_KEY');
$headers = ['Authorization: Bearer ' . $apiKey];
$client = new SendGrid
Client('https://api.sendgrid.com', $headers, '/v3');
```
--------------------------------
### SendGrid Email Configuration Example
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/USE_CASES.md
This comprehensive example demonstrates how to configure various aspects of an email, including global headers, sections, categories, reply-to addresses, ASM settings, IP pool names, mail settings (BCC, bounce, spam, unsubscribe, footer, sandbox, spam check), and tracking settings (click, open, subscription, Google Analytics). It concludes with sending the email using the SendGrid API and handling the response or exceptions.
```php
$email->addGlobalHeader(new Header("X-Day", "Monday"));
$globalHeaders = [
new Header("X-Month", "January"),
new Header("X-Year", "2017")
];
$email->addGlobalHeaders($globalHeaders);
$email->addSection(
new Section(
"%section1%",
"Substitution for Section 1 Tag"
)
);
$sections = [
new Section(
"%section3%",
"Substitution for Section 3 Tag"
),
new Section(
"%section4%",
"Substitution for Section 4 Tag"
)
];
$email->addSections($sections);
$email->addCategory(new Category("Category 1"));
$categories = [
new Category("Category 2"),
new Category("Category 3")
];
$email->addCategories($categories);
$email->setBatchId(
new BatchId(
"MWQxZmIyODYtNjE1Ni0xMWU1LWI3ZTUtMDgwMDI3OGJkMmY2LWEzMmViMjYxMw"
)
);
$email->setReplyTo(
new ReplyTo(
"dx+replyto2@example.com",
"DX Team Reply To 2"
)
);
$asm = new Asm(
new GroupId(1),
new GroupsToDisplay([1,2,3,4])
);
$email->setAsm($asm);
$email->setIpPoolName(new IpPoolName("23"));
$mail_settings = new MailSettings();
$mail_settings->setBccSettings(
new BccSettings(true, "bcc@example.com")
);
// Note: Bypass Spam, Bounce, and Unsubscribe management cannot
// be combined with Bypass List Management
$mail_settings->setBypassBounceManagement(
new BypassBounceManagement(true)
);
$mail_settings->setBypassSpamManagement(
new BypassSpamManagement(true)
);
$mail_settings->setBypassUnsubscribeManagement(
new BypassUnsubscribeManagement(true)
);
// OR
$mail_settings->setBypassListManagement(
new BypassListManagement(true)
);
$mail_settings->setFooter(
new Footer(true, "Footer", "Footer")
);
$mail_settings->setSandBoxMode(new SandBoxMode(true));
$mail_settings->setSpamCheck(
new SpamCheck(true, 1, "http://mydomain.com")
);
$email->setMailSettings($mail_settings);
$tracking_settings = new TrackingSettings();
$tracking_settings->setClickTracking(
new ClickTracking(true, true)
);
$tracking_settings->setOpenTracking(
new OpenTracking(true, "--sub--")
);
$tracking_settings->setSubscriptionTracking(
new SubscriptionTracking(
true,
"subscribe",
"subscribe",
"%%sub%%"
)
);
$tracking_settings->setGanalytics(
new Ganalytics(
true,
"utm_source",
"utm_medium",
"utm_term",
"utm_content",
"utm_campaign"
)
);
$email->setTrackingSettings($tracking_settings);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}
```
--------------------------------
### Parse PHP Code to AST and Dump
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/nikic/php-parser/README.md
Installs the library using composer and demonstrates parsing PHP code into an AST, then dumping the AST in a human-readable format.
```php
create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse($code);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
return;
}
$dumper = new NodeDumper;
echo $dumper->dump($ast) . "\n";
```
--------------------------------
### Install PHP Text Template as a Project Dependency
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/phpunit/php-text-template/README.md
Use this command to add the library as a local, per-project dependency using Composer.
```bash
composer require phpunit/php-text-template
```
--------------------------------
### Install PHP Invoker as a Project Dependency
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/phpunit/php-invoker/README.md
Use this command to add the library as a local, per-project dependency.
```bash
composer require phpunit/php-invoker
```
--------------------------------
### Install PSR Log Package
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/psr/log/README.md
Use Composer to install the PSR Log package.
```bash
composer require psr/log
```
--------------------------------
### Clone and Install PHP HTTP Client
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/vendor/sendgrid/php-http-client/CONTRIBUTING.md
Clone the SendGrid PHP HTTP Client repository and navigate into the project directory to begin local development.
```bash
git clone https://github.com/sendgrid/php-http-client.git
cd php-http-client
```
--------------------------------
### Copy Environment Sample File
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/sendgrid/README.md
Copy the sample environment file to `.env` to configure your SendGrid API key. This is a common practice for managing sensitive credentials.
```bash
cp .env.sample .env
```
--------------------------------
### Composer Installation (CLI)
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/PHPMailer/README.md
Run this command in your terminal to install PHPMailer using Composer.
```sh
composer require phpmailer/phpmailer
```
--------------------------------
### Install php-code-coverage with Composer
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/phpunit/php-code-coverage/README.md
Add the php-code-code-coverage library as a project dependency using Composer.
```bash
composer require phpunit/php-code-coverage
```
--------------------------------
### Initial Mocking Attempt (Fails)
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/elasticsearch/vendor/mockery/mockery/docs/cookbook/mocking_class_within_class.rst
Demonstrates an initial attempt to mock App\Point and App\Rectangle. This approach fails because App\Point is already autoloaded by the time the mock is set up.
```php
shouldReceive("setPoint")->andThrow(Exception::class);
$rect = Mockery::mock("App\Rectangle")->makePartial();
$rect->shouldReceive("draw");
$rect->create(0, 0, 100, 100); // does not throw exception
Mockery::close();
}
}
```
--------------------------------
### Composer Installation (JSON)
Source: https://github.com/cubecart/v6/blob/v6.x-master/classes/PHPMailer/README.md
Add this line to your composer.json file to install PHPMailer using Composer.
```json
"phpmailer/phpmailer": "^6.2"
```