| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace DNADesign\Elemental\Tests\Behat\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use DNADesign\Elemental\Extensions\ElementalAreasExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DNADesign\Elemental\Extensions\ElementalPageExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DNADesign\Elemental\Models\BaseElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DNADesign\Elemental\Models\ElementContent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\BehatExtension\Utility\StepHelper; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\ClassInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Core\Extensible; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Dev\FixtureBlueprint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\ORM\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\ORM\HasManyList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | if (!class_exists(BaseFixtureContext::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Context used to create fixtures in the SilverStripe ORM. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class FixtureContext extends BaseFixtureContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @Given /(?:the|a) "([^"]+)" "([^"]+)" (?:with|has) a "([^"]+)" content element with "([^"]+)" content/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $pageTitle | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $elementTitle | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $elementContent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function theHasAContentElementWithContent($type, $pageTitle, $elementTitle, $elementContent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         // Create the page (ElementalArea is created on write and attached to it) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $targetClass = $this->convertTypeToClass($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $page = $this->getFixtureFactory()->get($targetClass, $pageTitle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (!$page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $page = $this->getFixtureFactory()->createObject($targetClass, $pageTitle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $elementalArea = $page->ElementalArea(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $elementalArea->Elements()->add($this->getFixtureFactory()->createObject(ElementContent::class, $elementTitle)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // Create element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $element = $this->getFixtureFactory()->get(ElementContent::class, $elementTitle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if ($element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $element->HTML = $elementContent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $element->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $element = $this->getFixtureFactory()->createObject(ElementContent::class, $elementTitle, $fields); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $element->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.