Object Oriented Programming · PHP

A SOLID Basis for the Factory Pattern

Benefits of building your code with a Factory Pattern

In an effort to keep our work DRY (Don’t repeat yourself), it is a best practice to use design patterns. The Factory pattern is often used in the creation of objects. A developer can do all of the work of creating the object in the factory, instead of repeating it every time you want to create a new instance.

If you later need to change, rename, or replace the class responsible for the object building…


…you can do so and you will only have to modify the code in the factory. Not having to to hunt down every place in your project that uses the class to change the code can be a real benefit.

Using the factory pattern is not always needed or the “right thing to do”. At times a factory will simply be adding unneeded complexity.

However when making a fairly large or complex project you may save a lot of trouble down the road by using factories.

Why Factory Method Pattern

  • Encapsulate the logic for instantiating complex objects.
  • Reduce tight coupling between our application classes.
  • Makes it easier to add additional types of creation classes.

How SOLID is the Factory Pattern?

[S] The Factory Pattern permits each subclass to focus on the single responsibility of building a specific object type.

[O] With the Factory Method Pattern, Well tested base classes are easily extended without breaking the existing object building logic. Building with TTD (Test Driven Design) or at least a reasonable degree of Unit testing, the Factory Method Pattern is open to extension while remaining closed to modification.

[L] The Factory Method Pattern employs subclasses, implementing the base class’s methods and properties. The subclasses conform to the base class’s implied behavior. By this we can see that the Factory Method Pattern satisfies the Liskov Substitution Principle.

[ I ] When designing a Factory Method Pattern, the developer should promote the Interface Segregation Principle by making sure that abstractions such as abstract base classes and interfaces do not unnecessarily impose methods that could be added by concrete classes.

[D] The Factory Method Pattern is designed to decouple the lower lever concerns associated with the construction of specific objects from the higher level logic involved in making the decision to build objects.

How to implement the Factory Method Pattern

While not a requirement, it is good to start with a class having a name appended with “Factory” to make the pattern obvious. In this example, the class is making products so the name is ProductFactory. Making your pattern obvious is a way of documenting your code and making it easier to find and extend the pattern.

The factory class should also have an obvious build method name such as “build” or “create” that takes parameters. The parameters passed into the build method will be used to make determinations about which concrete classes to use in constructing new objects. Design the parameters with extensibility in mind. Often, is it best to package parameters into arrays so more or less parameters may be used without altering the basic structure of the original builder method. It often makes sense for the factory method pattern to be static or available as a service to other classes.

Create a Factory class

In this example, the concrete classes will be built based on abstraction in order to keep them consistent with the “contracted” properties and methods for the type of objects being built.

The abstraction of product defaults

The variations in the objects are built in the various concrete classes. Additional concrete classes may easily be built to satisfy future requirements without breaking the existing logic.

The concrete maker classes

Additional concrete classes may easily be built to satisfy future requirements without breaking the existing logic.

How To · PostgreSQL · Symfony 4

Configure Symfony 4 with PostgreSQL on Ubuntu or Mac

How to configure Symfony 4 with PostgreSQL on Ubuntu or Mac OS

Install PostgreSQL on Ubuntu  
Easily install PostgreSQL from the command line with apt-get.

Install PostgreSQL on Mac   
On Mac OS, go to your terminal and use brew as shown here.
Use composer to install Doctrine   
If Doctrine is not already installed, then from the command line in your Symfony 4 project directory, use composer to require doctrine.
Configure config/doctrine.yml
Change the dbal values as shown below.

Edit the .env file.   
PostgreSQL may create a user matching the user you logged in as. You will need to change the db_user and password to whatever is appropriate. Replace the mysql settings with the PostgreSQL settings as shown below:

Now you can use the command line to create the database!
AWS · How To

AWS Elastic Beanstalk [Resolved]: “… not authorized to perform: iam:CreateServiceLinkedRole on resource …”

How to resolve the Elastic Beanstalk Error 

not authorized to perform CreateServiceLinkedRole error

ERROR

Resolved by adding the following inline policy to the group the user was attached to via IAM :

Object Oriented Programming · PHP

How SOLID is the STRATEGY PATTERN?

STRATEGY PATTERN

In a strategy pattern the main value is in being able to switch out different algorithms for different situations.

Imagine that you had an application component that renders graphs for a website depending on what kind of device made the request. The application code could detect the type of browser and then use the strategy pattern to determine how to render the graphs. By implementing the strategy pattern, the application handles the “choice” in way that is extensible.

WHEN TO USE:

When several algorithms (ways to perform an operation) exist and the application should decide the specific way based on parameters.

Some examples:

  • A user registration may need to decide between different profile types.
  • A component that renders graphs depending on the requesting source
  • A CMS may need to create different content types.
  • An application may take payment in different ways.

How SOLID is the strategy Pattern?

[S] The Strategy Pattern permits each subclass to focus on a single responsibility.

[O] If the application was build with TTD (Test Driven Design) or at least involved a reasonable degree of Unit testing, then it is open to extension while remaining closed to modification.

[L] Because the strategize subclasses are implementing an interface, they satisfy the Liskov Substitution Principle.

[ I ] Although by design, the Strategy Pattern should promote the Interface Segregation Principle, it is up to the developer to make sure that is maintained.

[D] The Strategy Pattern quite intentionally decouples the algorithms involved with handling specific situations and their lower lever concerns from the higher level logic that originally “decided” to implement a given component.

How to implement the Strategy Pattern?

Create a well tested interface the meets all the requirements of the application while remaining wide open to extension. Since this interface will be the basis for future logic, it is important to make sure to include the minimum requirements needed by all situations. Do not try to cover all the bases here. Instead leave room for addition interfaces if needed. If you try to cover all the bases, you end up forcing a method where it may not be needed – violating the Interface Segregation Principle.

Create classes that implement the interface and handle more specific situations. These are the classes that will be implemented once the application logic make a “decision” on which one is appropriate.

Create logic, preferably iterative logic with a good naming convention, to handle application “choices” of which strategy to implement.


Use iterative logic

If your decision making logic has used iterative logic with a good naming convention, the coding footprint will remain small. Using iterative logic when possible instead of if statements or switch statements will keep the code from growing with every additional extension of the strategy. Everyone likes nice neat code, right?

Well documented code tells the story

A good documentation style is important to keeping the code readable. Well documented code will help make the iterative logic handling the strategy routing easier to work with. Be sure to use classnames and method names that “tell the story”. Making your coding style communicate to yourself or other developers can save a lot of time. In the future, you or another developer, can further create classes that extend the original strategic interface to handle other more specific situations.

Docker · Solution to Error

[RESOLVED] Docker Error: Error starting userland proxy: Bind for 0.0.0.0:80: unexpected error (Failure EADDRINUSE)

If you are trying to run Docker on your Mac and you get this error:

 

Check to see if Apache is running on your Host computer (Mac in my case)

Stopping Apache on your mac should fix this error;

eg

Hope this helps someone!

How To

How to Install Mono and Run KeePass.exe on Mac

Do not install with HomeBrew. HomeBrew has issues with mono and when a thread was opened to address the issues homebrew monitor closed the thread without resolving the issue saying he did not like the tone of the thread.

After Installing Mono via this download link from the official Mono site, mono may be used from the command line.

NOTE:  The path for using mono is:

/Library/Frameworks/Mono.framework/Versions/Current/bin/mono

You can alter your .bash_profile file to make this easier with an alias.

Install KeePass as instructed at this link .

Be sure to

  1. Download the portable version of KeePass (file KeePass-2.xx.zip) and unpack it in a location of your choice.
  2. From the KeePass installed directory, run the command $> /Library/Frameworks/Mono.framework/Versions/Current/bin/mono –arch=32 KeePass.exe
  3. ref: Stack Exchange thread: https://stackoverflow.com/questions/32542535/how-to-install-mono-on-macos-so-mono-works-in-terminal
MAMP · Solution to Error · Windows Server

[RESOLVED] WAMP Windows Apache document root configured for shared directory on Mac OSX

No matter what I tried I could not configure the Apache DocumentRoot in Apache on Windows via WAMP to see a folder that had been shared by my MAc OS by way of VM Ware Fusion.

I searched all over the internet and was unable to resolve this until setting up a Windows symlink in addition addition to the shared folder.

How to Configure Apache Virtual Host DocumentRoot for a Shared Folder on Mac OS with VM Ware Fusion

Prerequisits

  1. Install and configuring WAMP
  2. Edit the hosts file (c:\windows\system32\drivers\etc\hosts) to point at my-site.dev (your locall dev domain)
  3. edit the the httpd-vhosts.conf file. Mine was located at:      C:\xampp\apache\conf\extra\httpd-vhosts.conf
Notice the document root is configured to point at
“C:/xampp/htdocs/some-fake-dir”.
However the directory named is not actually there (sort of). This is where we can use a symlink to help Apache work with the directory shared from your Mac OS via VMWare Fusion.

Windows Apache on Vmware Fusion Needed a little Help to see the shared directory
Refer to the following 2 articles (don’t worry they’re short and to the point) in order to get Windows SymLinks working on your virtual machine:

Create the symlink to help VMWare Fusion see the shared folder

  1. Right-click on “Command Prompt” (AKA Terminal) and select “Run as Administrator”
  2. Use the mklink command to create a symlink in Windows on your virtual Machine that points from to the shared folder pointing back to your Mac OS shared directory that you wish to use for the document root.
  3. NOTE: The shared folder takes the form “Z:\<some-shared-folder>”

Restart Apache and Test
Once the http-vhosts file has been configured to point at the shared folder, and the ServerName (my-site.dev) has been set to point at the domain you have configured in the hosts file, you should be able to view your site file by navigating to http://my-site.dev in the Windows browser on your virtual machine.

Apache · Backend · WordPress

Mac Locahost uploads broken after Mac OS Upgrade

Before the recent MAC OS upgrade, wordpress plugin updates were working fine on my local development environment.

Then I started getting messages like “upload failed server error” when attempting to update wordpress plugins from my local hosted wordpress sites.

At first, I made sure the permissions were correct and that the files and directories had the proper owner. But this is did not fix the issue.

This is when I realized that something must have changed in the apache set up.

For now, I am still using PHP 5 on these local servers (soon switching to 7)

The apache configuration file had been silently altered by the Mac OS update!

Apple makes a backup of the existing apache configuration file before replacing it.

Performing a command line diff, I was able to see what had changed.

LoadModule php5_module libexec/apache2/libphp5.so had been commented out!

Uncommented the load module command for php5_module (or whatever PHP version you are using) finally restored the upload functionality of my local server.

I hope this helps someone find the answer to this issue a little faster!

PHP · Solution to Error

operator not supported for strings slider_markup_init.php [SOLVED]

This error comes from an older version of the   WordPress LayerSlider plugin after an upgrade to PHP 7.

In case you are unable to upgrade your LayerSlider plugin, or do not wish to,  here is a patch to fix the issue. Don’t worry it’s pretty easy.  : )

How to resolve error: operator not supported for strings slider_markup_init.php

Locate the file. Your particular error will tell you where the file is located. The file is usually located in the following directory:

/wp-content/plugins/LayerSlider/includes/slider_markup_init.php       
The line number is likely line 84.

 

This error results from using the short array push syntax on a string.

How to fix the PHP 7 ERROR in slider_markup_init.php